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

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. Selenium Grid的原理、配置与使用(转)

    Selenium GridSelenium Grid在前面介绍Selenium的时候说过它有三大组件,Selenium Grid就是其中之一而作用就是分布式执行测试.讲分布式之前还是要说说UI自动化的 ...

  2. No configuration file found and no output filename configured via Cli option.报错

    webpack手动配置webpack.config.js文件,打包时出现的报错,可以试试这种解决方案 报错如下: No configuration file found and no output f ...

  3. 如何完美获得一个double值的整数部分

    如果是java有float类型的向上取整:Math.ceil() //只要有小数都+1向下取整:Math.floor() //不取小数四舍五入:Math.round() //四舍五入 如果是C++:方 ...

  4. Unity ugui Anchor锚点自动适配画布中的相对位置

    本随笔参考了以下博客,在此基础上进行优化和改进: https://blog.csdn.net/qq_39640124/article/details/88284191 ugui中的Anchor预设如下 ...

  5. 浅谈pyautogui模块

    pyautogui模块 PyAutoGUI--让所有GUI都自动化 安装代码: pip install pyautogui 目的 PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以 ...

  6. numpy模块&pandas模块

    目录 numpy模块 pandas模块 numpy模块 import pandas as pd import numpy as np df=pd.Series(np.array(['a','b'])) ...

  7. SQL注入原理及代码分析(一)

    前言 我们都知道,学安全,懂SQL注入是重中之重,因为即使是现在SQL注入漏洞依然存在,只是相对于之前现在挖SQL注入变的困难了.而且知识点比较多,所以在这里总结一下.通过构造有缺陷的代码,来理解常见 ...

  8. 每日一题 - 剑指 Offer 36. 二叉搜索树与双向链表

    题目信息 时间: 2019-06-29 题目链接:Leetcode tag: 二叉搜索树 中序遍历 递归 深度优先搜索 难易程度:中等 题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循 ...

  9. idea 快速生成返回值快捷方式

    idea java快速生成返回值   ctrl+alt+V

  10. 底层剖析 Window 、Activity、 View 三者关系

    不管工作几年的 Android 工程师,或多或少都听说过 Window 的概念,并且隐隐约约感觉它在 Activity 与 View 之间应该发挥着某种连接的作用.但是如果需要说出这 3 者之间的关系 ...