前天無心間在網上看到百度ApiStore,而後好奇就進去看了看。正好最近在學習Android,剛學到java基礎。抱着鍛鍊的心態選擇手機號碼歸屬地查詢api進行練手。api地址 (http://apis.baidu.com/apistore/mobilephoneservice/mobilephone)。百度官方已經給出請求示例 。咱們只須要對請求結果json進行解析就能夠。java
Java請求示例:json
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 httpArg 10 * :參數 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) url 22 .openConnection(); 23 connection.setRequestMethod("GET"); 24 // 填入apikey到HTTP header 25 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進行解析輸出。api
百度給出的json返回示例:app
1 { 2 errNum: 0, 3 errMsg: "success", 4 retData: { 5 telString: "15846530170", //手機號碼 6 province: "黑龍江", //省份 7 carrier: "黑龍江移動" //運營商 8 } 9 }
對json解析須要用到json-lib.jar包,網上能夠百度到。學習
json解析核心代碼:url
1 JSONObject obj = JSONObject.fromObject(jsonResult1); 2 String errNum = obj.getString("errNum"); spa
演示示例:.net
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 args 15 */ 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 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 urlAll 39 * :請求接口 40 * @param httpArg 41 * :參數 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) url 53 .openConnection(); 54 connection.setRequestMethod("GET"); 55 // 填入apikey到HTTP header 56 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 }
輸出結果:code
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 錯誤碼:0 3 錯誤碼返回:success 4 你查詢號碼:15737954118 5 歸屬地:河南 6 運營商:河南移動
自此百度手機號碼歸屬地api體驗成功結束,也是實訓期間的練手。blog