手机号码归属地查询免费api接口代码

时间:2021-05-31 21:44:27

依据手机号码查询用户的卡类型、运营商、归属地、区域等信息。

手机归属地实体类

package org.wx.xhelper.model;

/**
* 手机归属地
* @author wangxw
* @version 1.0
* @date Jul 9, 2014 4:03:04 PM
*/
public class PhoneBelong { // 电话号码
private String phone; // 手机号码所在地区区号
private String area; // 号码卡类型
private String ctype; // 所属运营商
private String operators; // 简写归属地
private String simcall; public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getArea() {
return area;
} public void setArea(String area) {
this.area = area;
} public String getCtype() {
return ctype;
} public void setCtype(String ctype) {
this.ctype = ctype;
} public String getOperators() {
return operators;
} public void setOperators(String operators) {
this.operators = operators;
} public String getSimcall() {
return simcall;
} public void setSimcall(String simcall) {
this.simcall = simcall;
}
}

手机归属地服务接口类

package org.wx.xhelper.service;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.wx.xhelper.model.PhoneBelong; /**
* 手机归属地服务接口类
* @author wangxw
* @version 1.0
* @date Jul 9, 2014 4:07:11 PM
*/
public class PhoneBelongService { /**
* 生成归属地相关信息
* @param phone
* @return 归属地相关信息
* @throws UnsupportedEncodingException
*/
public static String getPhoneBelongDetail(String phone) throws UnsupportedEncodingException{
// 获取手机归属地
PhoneBelong phoneBelong = getPhoneBelong(phone); // 存储文本信息
StringBuffer news = new StringBuffer(); if (phoneBelong != null) {
news.append("号码:"+phoneBelong.getPhone()).append("\n");
news.append("区号:"+phoneBelong.getArea()).append("\n");
news.append("卡类型:"+phoneBelong.getCtype()).append("\n");
news.append("运营商:"+phoneBelong.getOperators()).append("\n");
news.append("归属地:"+phoneBelong.getSimcall()).append("\n\n");
} if(news.length() == 0){
news.append("号码").append(phone).append("不存在,请又一次输入!");
} // 截取字符串以免超出微信最大发送字符数2048
if(news.toString().getBytes("UTF-8").length > 2048){
return news.substring(0, 2000/3).concat("...");
} return news.toString();
} /**
* 获取手机归属地
* @param phone
* @return 手机归属地对象
*/
public static PhoneBelong getPhoneBelong(String phone){
URL url = null;
PhoneBelong phoneBelong = new PhoneBelong();
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); url = new URL("http://api.k780.com:88/? app=phone.get&phone="+phone+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml"); Document doc = builder.parse(url.openStream());
NodeList node = doc.getElementsByTagName("result"); for(int i=0;i<node.getLength();i++){
String area = "";
String ctype = "";
String operators = "";
String simcall = "";
if(doc.getElementsByTagName("area").item(i).getFirstChild() != null){
area = doc.getElementsByTagName("area").item(i).getFirstChild().getNodeValue();
}
if(doc.getElementsByTagName("ctype").item(i).getFirstChild() != null){
ctype = doc.getElementsByTagName("ctype").item(i).getFirstChild().getNodeValue();
}
if(doc.getElementsByTagName("operators").item(i).getFirstChild() != null){
operators = doc.getElementsByTagName("operators").item(i).getFirstChild().getNodeValue();
}
if(doc.getElementsByTagName("style_simcall").item(i).getFirstChild() != null){
simcall = doc.getElementsByTagName("style_simcall").item(i).getFirstChild().getNodeValue();
}
phoneBelong.setPhone(phone);
phoneBelong.setArea(area);
phoneBelong.setCtype(ctype);
phoneBelong.setOperators(operators);
phoneBelong.setSimcall(simcall);
} }catch(Exception e){
e.printStackTrace();
}
return phoneBelong;
}
}

查询结果:

号码:13800138000

区号:010

卡类型:移动全球通卡

运营商:移动

归属地:中国,北京