码迷,mamicode.com
首页 > 移动开发 > 详细

手机号码归属地API

时间:2015-04-25 14:54:15      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

今天用到了查询手机号码归属地的这个功能,但是公司自己的字典库数据太过陈旧,每一百个号码就有五六个识别不出来,所以就找了开源的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&timestamp="+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;

 

手机号码归属地API

标签:

原文地址:http://www.cnblogs.com/siyu/p/4455795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!