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();
}
}
}

  

最新文章

  1. iOS第三方库管理工具
  2. [SDK2.2]Windows Azure Virtual Network (5) 重启、关闭、开启VNet中Virtual Machine
  3. Spring-程序中获取注册bean的方式
  4. Java计时器Timer和TimerTask用法
  5. iOS本地数据存取
  6. 转载总结 C# 多态(虚方法,抽象,接口实现)
  7. PowerDesigner 15 概述
  8. js基础小总结之string&array&object
  9. 本博客弃用,请移步http://ningios.com查看最新
  10. Address already in use: JVM_Bind<null>:8080tomcat启动不了的问题
  11. dom4j解析xml实例
  12. sql2012笔记
  13. IntelliJ IDEA中 todo的使用
  14. APP压力测试 monkey(新猿旺学习总结)
  15. python第五十九天-----补上笔记
  16. 初识numpy
  17. 2.17 C++类与const关键字
  18. 【葡萄城报表】还在为画“类Word文档报表”而发愁吗?
  19. POP3_使用SSL链接邮箱并获取邮件
  20. java中的null和""区别------&&与&的区别

热门文章

  1. simplified build configuration
  2. Configurataion Printer(基于全新2.2.0API)
  3. .net软件自动化测试笔记(API-1)
  4. 如何组建理想SOA团队
  5. SDWebImage源码解读之SDWebImageDownloader
  6. SQL Server中CURD语句的锁流程分析
  7. xcodebuild导出ipa方法
  8. Android UI -- 内容简介
  9. nyoj开心的小明
  10. Git 钩子