国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

百度手機號碼歸屬地查詢api與返回json處理

2019-11-15 00:54:09
字體:
來源:轉載
供稿:網友
百度手機號碼歸屬地查詢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體驗成功結束,也是實訓期間的練手。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜兴市| 平遥县| 象州县| 和硕县| 赣州市| 外汇| 容城县| 舒兰市| 大名县| 自贡市| 那坡县| 钟祥市| 朝阳市| 龙门县| 和林格尔县| 舒兰市| 兴国县| 金川县| 长武县| 温泉县| 拉萨市| 托克托县| 婺源县| 顺义区| 准格尔旗| 南华县| 罗源县| 许昌市| 靖江市| 京山县| 黄大仙区| 梅河口市| 汉寿县| 逊克县| 五常市| 乌兰县| 洛浦县| 隆昌县| 盐山县| 仁怀市| 都昌县|