今天用到了查询手机号码归属地的这个功能,但是公司自己的字典库数据太过陈旧,每一百个号码就有五六个识别不出来,所以就找了开源的API,维护了一下字典库。
用到的API有两个,整理如下:
淘宝的
JSONObject json = null; String url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=17001090102"; StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "GBK")); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); json = JSONObject.fromObject(response.substring(response.indexOf("{"), response.indexOf("}")+1)); } else { System.out.println("-------------查询归属地错误----------------"); } } catch (Exception e) { e.printStackTrace(); System.out.println("-------------查询归属地错误----------------"); } finally { method.releaseConnection(); } return json;
手机中国的
JSONObject json = null; String url = "http://v.showji.com/Locating/showji.com20150416.aspx?m="+phoneNum+"&output=json&callback=querycallback×tamp="+Calendar.getInstance().getTimeInMillis(); StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "utf-8")); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); json = JSONObject.fromObject(response.substring(response.indexOf("{"), response.indexOf("}")+1)); } else { System.out.println("-------------查询归属地错误----------------"); } } catch (Exception e) { e.printStackTrace(); System.out.println("-------------查询归属地错误----------------"); } finally { method.releaseConnection(); } return json;
时间: 2024-11-23 10:05:43