你的位置:首页 > 软件开发 > Java > 百度手机号码归属地查询api与返回json处理

百度手机号码归属地查询api与返回json处理

发布时间:2015-07-16 19:00:11
前天无意间在网上看到百度ApiStore,然后好奇就进去看了看。正好最近在某博培训Android,刚学到java基础。抱着锻炼的心态选择手机号码归属地查询api进行练手。api地址 (http://apis.baidu.com/apistore/mobilephoneservic ...

     前天无意间在网上看到百度ApiStore,然后好奇就进去看了看。正好最近在某博培训Android,刚学到java基础。抱着锻炼的心态选择手机号码归属地查询api进行练手。api地址 (http://apis.baidu.com/apistore/mobilephoneservice/mobilephone)。百度官方已经给出请求示例 。我们只需要对请求结果json进行解析就可以。

 Java请求示例:

 1 String httpUrl = "http://apis.baidu.com/apistore/mobilephoneservice/mobilephone"; 2 String httpArg = "tel=15846530170"; 3 String jsonResult = request(httpUrl, httpArg); 4 System.out.println(jsonResult); 5  6 /** 7  * @param urlAll 8  *      :请求接口 9  * @param httpArg10  *      :参数11  * @return 返回结果12 */13 public static String request(String httpUrl, String httpArg) {14   BufferedReader reader = null;15   String result = null;16   StringBuffer sbf = new StringBuffer();17   httpUrl = httpUrl + "?" + httpArg;18 19   try {20     URL url = new URL(httpUrl);21     HttpURLConnection connection = (HttpURLConnection) url22         .openConnection();23     connection.setRequestMethod("GET");24     // 填入apikey到HTTP header25     connection.setRequestProperty("apikey", "您自己的apikey");26     connection.connect();27     InputStream is = connection.getInputStream();28     reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));29     String strRead = null;30     while ((strRead = reader.readLine()) != null) {31       sbf.append(strRead);32       sbf.append("\r\n");33     }34     reader.close();35     result = sbf.toString();36   } catch (Exception e) {37     e.printStackTrace();38   }39   return result;40 }

原标题:百度手机号码归属地查询api与返回json处理

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。