java 手机号码归属地查询
下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言)
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 手机号码归属地查询的更多相关文章
- 【原创】Java实现手机号码归属地查询
网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...
- 本地的手机号码归属地查询-oracle数据
最近做的项目中,有个功能是手机归属地查询,因为项目要在内网下运行,所以不能用提供的webservice,只好在网上找手机归属地的数据,很多都是access的,我们的项目是用oracle,只好自己转吧, ...
- 百度手机号码归属地查询api与返回json处理
前天无意间在网上看到百度ApiStore,然后好奇就进去看了看.正好最近在某博培训Android,刚学到java基础.抱着锻炼的心态选择手机号码归属地查询api进行练手.api地址 (http://a ...
- 免费的手机号码归属地查询API接口文档
聚合数据手机号码归属四查询API接口,根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 通过链接https://www.juhe.cn/docs/ ...
- PHP实现 手机号码归属地查询
在工作中需要对手机号码进行归属地查询,就在网上找了下解决方案,发现通过号段非常好判断. 我将数据库文件放到了百度网盘,请有需要的朋友去下载. 链接: https://pan.baidu.com/s/1 ...
- C# Winform实现手机号码归属地查询工具
摘要:本文介绍使用C#开发基于Winform的手机号码归属地查询工具,并提供详细的示例代码供参考. 一.需求描述 输入正确的手机号码,查询该号码的归属地和其他相关信息. 二.需求分析 1.实现手机号码 ...
- 手机号码归属地查询免费api接口代码
依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...
- 手机号码归属地查询api接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
- PHP手机号码归属地查询API接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
随机推荐
- 阿里druid数据源属性配置表
https://cloud.tencent.com/developer/article/1368903 DRUID 属性说明表 属性(Parameter) 默认值(Default) 描述(Descri ...
- drf-Authentication认证
drf-Authentication认证 ## 源码分析 ```python """ 1)APIView的dispath(self, request, *args, ** ...
- Oracel中coalesce函数的用法
1.coalesce函数的用法 1.1 取出第一个不为空的列的数据.
- 使用docker创建mysql容器
1.拉取mysql容器 docker pull mysql:5.7
- Java BigDecimal和double BigDecimal类
BigDecimal类 对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数 ...
- hive sql 解析json
在hive中会有很多数据是用json格式来存储的,而我们用数据的时候又必须要将json格式的数据解析成为正常的数据,今天我们就来聊聊hive中是如何解析json数据的. 下面这张表就是json格式的表 ...
- WSL中文本地化
WSL中文本地化 Windows Subsystem for Linux(简称WSL)是一个在Windows 10上能够运行原生Linux二进制可执行文件(ELF格式)的兼容层.它是由微软与Canon ...
- 「MoreThanJava」Day 1:环境搭建和程序基本结构元素
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 洛谷 P4910 帕秋莉的手环
题意 多组数据,给出一个环,要求不能有连续的\(1\),求出满足条件的方案数 \(1\le T \le 10, 1\le n \le 10^{18}\) 思路 20pts 暴力枚举(不会写 60pts ...
- BZOJ2200 道路与航线 题解
题目 Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 \((1 <= T <= 25,000)\),编号为\(1T\).这些城镇之间通过\(R ...