Baidu mobile phone number attribution to query API and return JSON processing

Source: Internet
Author: User

The day before yesterday inadvertently on the internet to see Baidu Apistore, and then curious to go in to see. Just recently in a blog training Android, just learned Java Foundation. Hold the mentality of exercise choose mobile phone number attribution to query API for practiced hand. API address (Http://apis.baidu.com/apistore/mobilephoneservice/mobilephone). Baidu official has given an example of the request. We only need to parse the request result JSON to be able to.

Java Request Example:

1String Httpurl = "Http://apis.baidu.com/apistore/mobilephoneservice/mobilephone";2String Httparg = "tel=15846530170";3String Jsonresult =request (Httpurl, httparg);4 System.out.println (jsonresult);5 6 /**7  * @paramUrlall8 *: Request interface9  * @paramHttpargTen *: Parameters One  * @returnreturn Results A  */ -  Public Staticstring Request (String Httpurl, String httparg) { -BufferedReader reader =NULL; theString result =NULL; -StringBuffer SBF =NewStringBuffer (); -Httpurl = Httpurl + "?" +Httparg; -  +     Try { -URL url =NewURL (httpurl); +HttpURLConnection connection =(httpurlconnection) URL A . OpenConnection (); atConnection.setrequestmethod ("GET"); -         //fill in Apikey to HTTP header -Connection.setrequestproperty ("Apikey", "Your own Apikey"); - Connection.connect (); -InputStream is =Connection.getinputstream (); -Reader =NewBufferedReader (NewInputStreamReader (IS, "UTF-8")); inString Strread =NULL; -          while((Strread = Reader.readline ())! =NULL) { to sbf.append (strread); +Sbf.append ("\ r \ n"); -         } the reader.close (); *result =sbf.tostring (); $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); -     } the     returnresult; +}

What we are going to do is to perform the result processing on the request return results, Baidu gives the return result as JSON, then the JSON will be parsed output.

example of a JSON return given by Baidu:

1 {2errnum:0,3errmsg: "Success",4retdata: {5     telstring: "15846530170",//Mobile number 6   province: "Heilongjiang",    //province 7     Carrier: "Heilongjiang mobile"  //Carrier 8  }9 }

For JSON parsing need to use Json-lib.jar package, online can Baidu to.

JSON parsing core code:

1 jsonobject obj = jsonobject.fromobject (JSONRESULT1); 2 String errnum = obj.getstring ("Errnum");

Demo Example:

1  Packageday02;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.InputStream;5 ImportJava.io.InputStreamReader;6 Importjava.net.HttpURLConnection;7 ImportJava.net.URL;8 ImportNet.sf.json.JSONObject;9 Ten  Public classTest11 { One  A     /** - * Check your mobile phone number to attribution -      * @paramargs the      */ -      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -  +String Httpurl = "Http://apis.baidu.com/apistore/mobilephoneservice/mobilephone"; -String Httparg = "tel=15768798455"; +String JSONRESULT1 =request (Httpurl, httparg); A System.out.println (JSONRESULT1); atJsonobject obj =Jsonobject.fromobject (JSONRESULT1); -String errnum = obj.getstring ("Errnum"); - System.out.println (errnum); -String errmsg = obj.getstring ("ErrMsg"); - System.out.println (errmsg); -String retdata = obj.getstring ("Retdata"); inJsonobject Obj2 =Jsonobject.fromobject (retdata); -String telstring = obj2.getstring ("telstring"); toString province = obj2.getstring ("province"); +String carrier = obj2.getstring ("Carrier"); -System.out.println ("Your Enquiry Number:" +telstring+ "\ n" + "Attribution:" +province+ "\ n" + "operator:" +carrier); the  *     } $ Panax Notoginseng     /** -      * @paramUrlall the *: Request interface +      * @paramHttparg A *: Parameters the      * @returnreturn Results +      */ -      Public Staticstring Request (String Httpurl, String httparg) { $BufferedReader reader =NULL; $String result =NULL; -StringBuffer SBF =NewStringBuffer (); -Httpurl = Httpurl + "?" +Httparg; the  -         Try {WuyiURL url =NewURL (httpurl); theHttpURLConnection connection =(httpurlconnection) URL - . OpenConnection (); WuConnection.setrequestmethod ("GET"); -             //fill in Apikey to HTTP header AboutConnection.setrequestproperty ("Apikey", $"Your Baidu API secret key"); - Connection.connect (); -InputStream is =Connection.getinputstream (); -Reader =NewBufferedReader (NewInputStreamReader (IS, "UTF-8")); AString Strread =NULL; +              while((Strread = Reader.readline ())! =NULL) { the sbf.append (strread); -Sbf.append (Strread + ""); $             } the reader.close (); theresult =sbf.tostring (); the}Catch(Exception e) { the e.printstacktrace (); -         } in         returnresult; the     } the  About}

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 return: Success4 Your enquiry number: 157379541185 attribution: Henan 6 operator: Henan Mobile

Since the Baidu mobile phone number attribution to the end of the API experience, but also the practiced hand during the training period.

Baidu mobile phone number attribution to query API and return JSON processing

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.