你的位置:首页 > Java教程

[Java教程]百度手机号码归属地查询api与返回json处理


     前天无意间在网上看到百度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 }

      我们要做的是进行对请求返回结果result处理,百度给出了返回结果为json,那么就要对json进行解析输出。

      百度给出的json返回示例:

1 {2 errNum: 0,3 errMsg: "success",4 retData: {5   telString: "15846530170", //手机号码6   province: "黑龙江",  //省份7   carrier: "黑龙江移动" //运营商8  }9 }

     对json解析需要用到json-lib.jar包,网上可以百度到。

     json解析核心代码:

  1 JSONObject obj = JSONObject.fromObject(jsonResult1); 2 String errNum = obj.getString("errNum"); 

     演示示例:

 

 1 package day02; 2  3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import net.sf.json.JSONObject; 9 10 public class Test11 {11 12   /**13    * 查询手机号码归属地14    * @param args15   */16   public static void main(String[] args) {17     // TODO Auto-generated method stub18 19     String httpUrl = "http://apis.baidu.com/apistore/mobilephoneservice/mobilephone";20     String httpArg = "tel=15768798455";21     String jsonResult1 = request(httpUrl, httpArg);22     System.out.println(jsonResult1);23     JSONObject obj = JSONObject.fromObject(jsonResult1);24     String errNum = obj.getString("errNum");25      System.out.println(errNum);26     String errMsg = obj.getString("errMsg");27      System.out.println(errMsg);28     String retData = obj.getString("retData");29     JSONObject obj2 = JSONObject.fromObject(retData);30     String telString = obj2.getString("telString");31     String province = obj2.getString("province");32     String carrier = obj2.getString("carrier");33     System.out.println("你查询号码:"+telString+"\n"+"归属地:"+province+"\n"+"运营商:"+carrier);34 35   }36 37   /**38    * @param urlAll39    *      :请求接口40    * @param httpArg41    *      :参数42    * @return 返回结果43   */44   public static String request(String httpUrl, String httpArg) {45     BufferedReader reader = null;46     String result = null;47     StringBuffer sbf = new StringBuffer();48     httpUrl = httpUrl + "?" + httpArg;49 50     try {51       URL url = new URL(httpUrl);52       HttpURLConnection connection = (HttpURLConnection) url53           .openConnection();54       connection.setRequestMethod("GET");55       // 填入apikey到HTTP header56       connection.setRequestProperty("apikey",57           "你的百度api秘钥");58       connection.connect();59       InputStream is = connection.getInputStream();60       reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));61       String strRead = null;62       while ((strRead = reader.readLine()) != null) {63         sbf.append(strRead);64         sbf.append(strRead + " ");65       }66       reader.close();67       result = sbf.toString();68     } catch (Exception e) {69       e.printStackTrace();70     }71     return result;72   }73 74 }

 

 输出结果:

 

 

1 {"errNum":0,"errMsg":"success","retData":{"telString":"15737954118","province":"\u6cb3\u5357","carrier":"\u6cb3\u5357\u79fb\u52a8"}}{"errNum":0,"errMsg":"success","retData":{"telString":"15737954118","province":"\u6cb3\u5357","carrier":"\u6cb3\u5357\u79fb\u52a8"}} 2 错误码:03 错误码返回:success4 你查询号码:157379541185 归属地:河南6 运营商:河南移动

 

自此百度手机号码归属地api体验成功结束,也是实训期间的练手。