java查询手机号码归属地
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查询手机号码归属地的更多相关文章
- 调用phone库,查询手机号码归属地(4)
需要安装pymysql,phone库 #!/usr/bin/python # -*- coding: utf-8 -*- import sys, pymysql, logging, phone fro ...
- 调用API接口,查询手机号码归属地(3)
从mysql数据库获取电话号码,查询归属地并插入到数据库 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib, sys, pym ...
- 调用API接口,查询手机号码归属地(2)
使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- 免费手机号码归属地API查询接口和PHP使用实例分享
免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...
- 免费手机号码归属地API查询接口
免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...
- 在java中如何根据手机号查询号码归属地
1.maven项目中配置 <dependency><groupId>com.googlecode.libphonenumber</groupId><artif ...
- 调用webservice查询手机号码归属地信息
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...
- 如何在Excel批量查询电话号码归属地?
手机号码归属地的重要性大家应该都清楚,如果取消或者更改手机号码归属地,那么一会增加用户的被诈骗风险;二是对套餐资费会产生影响,加剧企业间的竞争,加剧数字鸿沟;三是企业运营管理需要投入大量人力物力,是个 ...
随机推荐
- js中replace用法
js中replace的用法 replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则 ...
- MS dos版本
1981年,MS-DOS 1.0发行,作为IBM PC的操作系统进行捆绑发售,支持16k内存及160k的5寸软盘.在硬件昂贵,操作系统基本属于送硬件奉送的年代,谁也没能想到,微软公司竟会从这个不起眼的 ...
- c#等待所有子线程执行完毕方法
当我们在使用线程中,你会发现主线结束后子线程的结果才显示出来.现在我要等待所以子线程结束,然后在显示结果,怎么做呢? 方法如下: 1.使用 ManualResetEvent,代码如下: using ...
- Retrofit 2.0 throwing @Field parameters can only be used with form encoding
最近在学习Retrofit2.0想用Retrofit + Dagger2 + RxJava + ButterKnife一起使用重构项目 一方面自己的demo随着业务逻辑的增多 显得非常的乱 另一方面代 ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- 【原】 Spark中Worker源码分析(二)
继续前一篇的内容.前一篇内容为: Spark中Worker源码分析(一)http://www.cnblogs.com/yourarebest/p/5300202.html 4.receive方法, r ...
- lru cache java
http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html acm之家的讲解是在是好,丰富 import java.util.LinkedHas ...
- S3C2440触摸屏驱动实例开发讲解
出处:http://www.embeddedlinux.org.cn/html/yingjianqudong/ 一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--6 ...
- linux的nohup命令的用法
在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...
- NOIP2015 子串 (DP+优化)
子串 (substring.cpp/c/pas) [问题描述] 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个 互不重 叠 的非空子串,然后把这 k 个子串按照其在字 ...