下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言)

package com.test.yyc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements; public class PhoneNumberBelong {
public static void main(String[] args) {
String mobileNumber = "13333333333";
try {
//System.out.println(calcMobileCity(mobileNumber));
//System.out.println(queryMobileLocation(mobileNumber));
System.out.println(queryMobileLocationk780(mobileNumber)); //{address=中国,河北,秦皇岛, area_code=0335}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} public static String calcMobileCity(String mobileNumber)
throws MalformedURLException {
String result = "";
try {
String urlString = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="
+ mobileNumber;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "GBK")); String line;
while ((line = reader.readLine()) != null) {
result = result + line;
result = result + "\n";
}
reader.close();
if (!(StringUtils.isEmpty(result))) {
Pattern p = Pattern.compile("province:'([^',]*)");
Matcher m = p.matcher(result);
while (m.find()) {
result = m.group(1);
}
connection = null;
return result;
}
return "无此号记录!";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 使用k780公司的接口
* @param tel
* @return
* @throws Exception
*/
public static Map<String, String> queryMobileLocationk780(String tel) throws Exception {
Pattern pattern = Pattern.compile("1\\d{10}");
Matcher matcher = pattern.matcher(tel);
Map<String, String> resultMap = new HashMap<String, String>();
String address = "";
String areaCode = "";
if (matcher.matches()) {
String url = "http://api.k780.com:88/?app=phone.get&phone=" + tel +"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
String result = callUrlByGet(url, "UTF-8");
if (!(StringUtils.isEmpty(result))) {
JSONObject json = new JSONObject(result);
if(json.getString("success").equals("1")){ // 请求成功
JSONObject resultJson = json.getJSONObject("result");
if(resultJson.getString("status").indexOf("NOT") <= -1){
address = resultJson.getString("style_simcall");
areaCode = resultJson.getString("area");
} else {
address = "未知归属地";
}
} else { // 请求失败
address = "未知归属地";
areaCode = "";
}
} else {
address = "未知归属地";
areaCode = "";
} resultMap.put("address", address);
resultMap.put("area_code", areaCode);
return resultMap;
} return resultMap;
}
private static String callUrlByGet(String callurl, String charset) {
String result = "";
try {
URL url = new URL(callurl);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), charset)); String line;
while ((line = reader.readLine()) != null) {
result = result + line;
result = result + "\n";
}
reader.close();
connection = null;
} catch (Exception e) {
e.printStackTrace();
return "";
}
return result;
}
/**
* 通过解析 IP138网站的html代码来获取号码归属地信息
* @param mobile
* @return
*/
public static Map<String, String> queryMobileLocation(String mobile){
String url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile="+mobile;
Map<String, String> resultMap = new HashMap<String, String>();
try {
Document doc = Jsoup.connect(url).get();
try {
Elements els = doc.getElementsByClass("tdc2");
String address = els.get(1).text();
String areaCode = els.get(3).text();
String corp = els.get(2).text();
String postCode = els.get(4).text();
if(postCode != null && !"".equals(postCode)){
postCode = postCode.substring(0, 6);
} String[] addresss = address.split(" ");
String city = "";
String province = "";
if(addresss.length > 0){
province = addresss[0];
if(addresss.length > 1){
city = addresss[1];
} else {
city = "";
}
} else {
province = "";
city = "";
}
resultMap.put("province", province);
resultMap.put("city", city);
resultMap.put("areaCode", areaCode);
resultMap.put("corp", corp);
resultMap.put("postCode", postCode);
} catch (Exception e) {
e.printStackTrace();
resultMap.put("province", "");
resultMap.put("city", "");
resultMap.put("areaCode", "");
resultMap.put("corp", "");
resultMap.put("postCode", "");
}
} catch (IOException e) {
e.printStackTrace();
resultMap.put("province", "");
resultMap.put("city", "");
resultMap.put("areaCode", "");
resultMap.put("corp", "");
resultMap.put("postCode", "");
} return resultMap;
}
}

java 手机号码归属地查询的更多相关文章

  1. 【原创】Java实现手机号码归属地查询

    网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...

  2. 本地的手机号码归属地查询-oracle数据

    最近做的项目中,有个功能是手机归属地查询,因为项目要在内网下运行,所以不能用提供的webservice,只好在网上找手机归属地的数据,很多都是access的,我们的项目是用oracle,只好自己转吧, ...

  3. 百度手机号码归属地查询api与返回json处理

    前天无意间在网上看到百度ApiStore,然后好奇就进去看了看.正好最近在某博培训Android,刚学到java基础.抱着锻炼的心态选择手机号码归属地查询api进行练手.api地址 (http://a ...

  4. 免费的手机号码归属地查询API接口文档

    聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...

  5. PHP实现 手机号码归属地查询

    在工作中需要对手机号码进行归属地查询,就在网上找了下解决方案,发现通过号段非常好判断. 我将数据库文件放到了百度网盘,请有需要的朋友去下载. 链接: https://pan.baidu.com/s/1 ...

  6. C# Winform实现手机号码归属地查询工具

    摘要:本文介绍使用C#开发基于Winform的手机号码归属地查询工具,并提供详细的示例代码供参考. 一.需求描述 输入正确的手机号码,查询该号码的归属地和其他相关信息. 二.需求分析 1.实现手机号码 ...

  7. 手机号码归属地查询免费api接口代码

    依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...

  8. 手机号码归属地查询api接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

  9. PHP手机号码归属地查询API接口

    淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...

随机推荐

  1. 【原创】Linux中断子系统(四)-Workqueue

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  2. 苹果XR手机的音频体验测试总结

    苹果XR手机的音频   苹果XR算是苹果手机历史上一个里程碑的型号了,是苹果憋了两年的大招,连苹果9的称号就不要了.直接是X.说明苹果对它给予的希望很大.作为一个音频算法工程师,一直想体验一下XR的音 ...

  3. SpringBoot--防止重复提交(锁机制---本地锁、分布式锁)

    防止重复提交,主要是使用锁的形式来处理,如果是单机部署,可以使用本地缓存锁(Guava)即可,如果是分布式部署,则需要使用分布式锁(可以使用zk分布式锁或者redis分布式锁),本文的分布式锁以red ...

  4. java List的初始化

    今天在处理生成excel的时候用到了java的list,但是需要直接赋值固定的几个变量,如果先初始化然后add的方法: List<String> name = new ArrayList( ...

  5. Vue 封装axios(四种请求)及相关介绍(十三)

    Vue 封装axios(四种请求)及相关介绍 首先axios是基于promise的http库 promise是什么? 1.主要用于异步计算 2.可以将异步操作队列化,按照期望的顺序执行,返回符合预期的 ...

  6. Java synthetic

    读完这篇文章你将会收获到 synthetic fields synthetic method synthetic class 概述 上一篇 Java 枚举 提及到编译成 class 文件之后.编译器会 ...

  7. 为DLL文件添加强名称

    程序在编译时出现类似 "错误 1 程序集生成失败 -- 引用的程序集“XXXXXXXXXX”没有强名称" 这样的错误,是因为它不是强名称的,则需要进行以下操作: 例如:com.so ...

  8. Milk Pumping G&Milk Routing S 题解

    Milk Pumping G&Milk Routing S 双倍经验时间 洛谷P5837 [USACO19DEC]Milk Pumping G 洛谷P3063 [USACO12DEC]Milk ...

  9. pycharm远程连接vagrant虚拟机中mariadb数据库

    1.虚拟机数据库设置--重启数据库 (1)vi /etc/my.cnf bind = 0.0.0.0 (2).远程不能用root用户连,得新建用户 select host,user from mysq ...

  10. 带大家认识CSS层叠上下文/层叠等级的区别和意义

    什么是“层叠上下文” 层叠上下文(stacking context),是HTML中一个三维的概念.在CSS2.1规范中,每个盒模型的位置是三维的,分别是平面画布上的X轴,Y轴以及表示层叠的Z轴.一般情 ...