Baidu mobile phone number attribution query api and return json processing, mobile phone number attribution api

Source: Internet
Author: User

Baidu mobile phone number attribution query api and return json processing, mobile phone number attribution api

I accidentally saw Baidu ApiStore on the Internet the day before yesterday. Then I went in curious and looked at it. I recently trained Android in a blog and just learned the basics of java. With the exercise mentality, you can select the mobile phone number to search for the api. Api address (http://apis.baidu.com/apistore/mobilephoneservice/mobilephone ). Baidu has provided an official request example. We only need to parse the request result json.

Java request example:

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 *: Request interface 9 * @ param httpArg10 *: parameter 11 * @ return result 12 */13 public static String request (String httpUrl, String httpArg) {14 BufferedReader reader = null; 15 String resul T = 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 // enter apikey to HTTP header25 connection. setRequestProperty ("apikey", "Your Own 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}

What we need to do is to process the returned result of the request. Baidu gives the returned result as json, so we need to parse the json output.

Json return example provided by Baidu:

1 {2 errNum: 15846530170 errMsg: "success", 4 retData: {5 telString: "", // mobile phone number 6 province: "Heilongjiang", // province 7 carrier: "Heilongjiang mobile" // operator 8} 9}

For json parsing needs to use json-lib.jar package, Web can Baidu.

Core json parsing code:

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

Demo:

 

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 * query mobile phone number @ 14 * @ param args15 */16 public static void main (String [] args) {17 // TODO Auto-generated method stub18 19 String httpUrl = "http://apis.baid U.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. getSt Ring ("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 ("your query number:" + telString + "\ n" + ":" + province + "\ n" + "carrier:" + carrier ); 34 35} 36 37/** 38 * @ param urlAll39 *: Request interface 40 * @ param httpArg41 *: parameter 42 * @ return returned result 43 */44 public st Atic 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 // enter apikey to HTTP header56 connection. setRequestProperty ("apikey", 57 "Your Baidu api key"); 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}

 

Output result:

 

 

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 error code: 03 error code returned: success4 your query number: 157379541185: Henan 6 OPERATOR: Henan mobile

 

Since then, the api experience of Baidu mobile phone number has been successfully completed, and it is also a practice during the practical training.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.