手機號碼歸屬地查詢免費api介面程式碼
阿新 • • 發佈:2019-01-26
根據手機號碼查詢使用者的卡型別、運營商、歸屬地、區域等資訊。
手機歸屬地實體類
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
卡型別:移動全球通卡
運營商:移動
歸屬地:中國,北京