package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException; public class MobileLocationUtil {
/**
* 归属地查询
* @param mobile
* @return mobileAddress
*/
private static String getLocationByMobile(final String mobile) throws ParserConfigurationException, SAXException, IOException{
String MOBILEURL = " http://www.youdao.com/smartresult-xml/search.s?type=mobile&q=";
String result = callUrlByGet(MOBILEURL + mobile, "GBK");
StringReader stringReader = new StringReader(result);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputSource); if (!(document.getElementsByTagName("location").item(0) == null)) {
return document.getElementsByTagName("location").item(0).getFirstChild().getNodeValue();
}else{
return "无此号记录!";
}
}
/**
* 获取URL返回的字符串
* @param callurl
* @param charset
* @return
*/
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 += line;
result += "\n";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return result;
}
/**
* 手机号码归属地
* @param tel 手机号码
* @return 135XXXXXXXX,联通/移动/电信,湖北武汉
* @throws Exception
* @author JIA-G-Y
*/
public static String getMobileLocation(String tel) throws Exception{
Pattern pattern = Pattern.compile("1\\d{10}");
Matcher matcher = pattern.matcher(tel);
if(matcher.matches()){
String url = "http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=" + tel;
String result = callUrlByGet(url,"GBK");
StringReader stringReader = new StringReader(result);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputSource);
String retmsg = document.getElementsByTagName("retmsg").item(0).getFirstChild().getNodeValue();
if(retmsg.equals("OK")){
String supplier = document.getElementsByTagName("supplier").item(0).getFirstChild().getNodeValue().trim();
String province = document.getElementsByTagName("province").item(0).getFirstChild().getNodeValue().trim();
String city = document.getElementsByTagName("city").item(0).getFirstChild().getNodeValue().trim();
if (province.equals("-") || city.equals("-")) {
return (tel + "," + supplier + ","+ getLocationByMobile(tel));
}else {
return (tel + "," + supplier + ","+ province + city);
}
}else {
return "无此号记录!";
}
}else{
return tel+ ":手机号码格式错误!";
}
} public static void main(String[] args) {
try {
System.out.println(getMobileLocation("13838383838"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

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

  1. 调用phone库,查询手机号码归属地(4)

    需要安装pymysql,phone库 #!/usr/bin/python # -*- coding: utf-8 -*- import sys, pymysql, logging, phone fro ...

  2. 调用API接口,查询手机号码归属地(3)

    从mysql数据库获取电话号码,查询归属地并插入到数据库 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib, sys, pym ...

  3. 调用API接口,查询手机号码归属地(2)

    使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...

  4. 调用API接口,查询手机号码归属地(1)

    使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...

  5. 免费手机号码归属地API查询接口和PHP使用实例分享

    免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...

  6. 免费手机号码归属地API查询接口

    免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...

  7. 在java中如何根据手机号查询号码归属地

    1.maven项目中配置 <dependency><groupId>com.googlecode.libphonenumber</groupId><artif ...

  8. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...

  9. 如何在Excel批量查询电话号码归属地?

    手机号码归属地的重要性大家应该都清楚,如果取消或者更改手机号码归属地,那么一会增加用户的被诈骗风险;二是对套餐资费会产生影响,加剧企业间的竞争,加剧数字鸿沟;三是企业运营管理需要投入大量人力物力,是个 ...

随机推荐

  1. 正则表达式(javascript)学习总结

    正则表达式在jquery.linux等随处可见,已经无孔不入.因此有必要对这个工具认真的学习一番.本着认真.严谨的态度,这次总结我花了近一个月的时间.但本文无任何创新之处,属一般性学习总结. 一.思考 ...

  2. Case swapping

    Case swapping Description: Given a string, swap the case for each of the letters. e.g. CodEwArs --&g ...

  3. AHOI2009最小割

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1072  Solved: 446[Submit] ...

  4. -_-#【网站优化】AJAX

    如何让搜索引擎抓取AJAX内容? Ajax 缓存: 两个重要的事实 使用 AJAX 事件触发 AJAX 请求.不要产生多次请求. 对 AJAX 请求使用 GET 方法 Use GET for AJAX ...

  5. Android学习之路——简易版微信为例(一)

    这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...

  6. .Net 垃圾回收机制原理(一)

    英文原文:Jeffrey Richter 编译:赵玉开 链接:http://www.cnblogs.com/yukaizhao/archive/2011/11/23/dot_net_GC_1.html ...

  7. android 后台附件下载

    在service中通过在oncreat()中开启一个线程,轮训ArrayList<AttachmentTask> 我这个附件下载的任务list ,ArrayList<Attachme ...

  8. POJ1456 Supermarket 贪心

    贪心策略:一定先卖价值最大的,然后考虑卖当前的物品,卖的日期越靠后,越优,可以为以后的物品提供机会 #include <stdio.h> #include <string.h> ...

  9. 华为2015 简单 字典输入法 java

    题目摘自http://blog.csdn.net/dongyi91/article/details/38639915 写了2个小时,水平太菜了 入法的编码原理为:根据已有编码表,当输入拼音和数字后输出 ...

  10. [九度OJ]1431.Sort(寻找前m大数并排序)

    原题链接:http://ac.jobdu.com/problem.php?pid=1431 题目描述: 给你n个整数,请按从大到小的顺序输出其中前m大的数. 输入: 每组测试数据有两行,第一行有两个数 ...