首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

依据手机号码查询手机号码归属地一

2013-12-07 
根据手机号码查询手机号码归属地一??String result callUrlByGet(MOBILEURL + mobile, GBK)??StringR

根据手机号码查询手机号码归属地一
??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) throws Exception{
??System.out.println(getMobileLocation("186xxxxxxxx"));
?}
}

热点排行