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. 【Linux】理解setuid()、setgid()和sticky位

    详见: http://blog.csdn.net/m13666368773/article/details/7615125 Linux SETUID机制 (1)进程运行时能够访问哪些资源或文件,不取决 ...

  2. 【HDOJ】1204 糖果大战

    题目本身不难.类似于dp.f(i)表示手中现有i颗糖果赢的概率,则下一局赢的概率是p(1-q),下一局输的概率是q(1-p),下一句平手的概率是1-p(1-q)-q(1-p),平手包括两人均答对或答错 ...

  3. linux,Centos,bash: service: command not found

    很简单,这个问题是这样的,su 或者 su root:的话只是将当前身份转为root,用户shell并没有改变.所以有些系统命令不能使用. su -或者su -l或者su -l root,可以完全的将 ...

  4. 单元测试中Assert类

    一.Assert类的使用 1.Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.Visua ...

  5. 一例胜千言,详谈SQL Sever数据库锁

    1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...

  6. zookeeper服务器端管理工具

    zookeeper基本是基于API和console进行znode的操作,并没有一个比较方便的操作界面,这里也发现了taobao 伯岩写的一个工具,可以比较方便的查询zookeeper信息. 工具的开发 ...

  7. 处理.NET中的内存泄露

    Fabrice Marguerie是一位软件架构师和咨询师,他在MSDN发表了如何检测和避免.NET程序内存与资源泄漏的文章.此文章描述了编写.NET程序时可能发生的内存与资源泄漏,以及如何避免这些泄 ...

  8. selenium1.0和selenium2.0页面等待处理详解

    一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...

  9. 浅谈Xcode5和Xcode7在系统创建的文件夹和文件中的区别

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. 【CSS】Intermediate4:Background Images

    1. background:background-color url-background-image background-repeat(repeat/repeat-y/repeat-x/no-re ...