Java use Taobao API to read and write JSON to achieve the mobile phone ownership query function code _java

Source: Internet
Author: User
Tags stringbuffer

General inquiries about the contents of the cell phone should be very easy to save the JSON format, found Taobao's home to the Internet API, and the processing of JSON-related jar bag, do the phone attribution to the query function

Copy Code code as follows:

Package Com.think.java;

Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.util.ArrayList;
Import java.util.List;
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

public class Testmobilecity {

/**
* Test the mobile phone number from which city, using the API of Taobao
* @param mobilenumber Mobile phone number
* @return
* @throws malformedurlexception
*/
public static string Calcmobilecity (String mobilenumber) throws malformedurlexception{
String jsonstring = null;
Jsonarray array = null;
Jsonobject jsonobject = null;
String urlstring = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobilenumber;
StringBuffer sb = new StringBuffer ();
BufferedReader buffer;
URL url = new URL (urlstring);
try{
InputStream in = Url.openstream ();

Solve garbled problem
Buffer = new BufferedReader (new InputStreamReader (in, "gb2312"));
String line = null;
while (line = Buffer.readline ())!= null) {
Sb.append (line);
}
In.close ();
Buffer.close ();
System.out.println (Sb.tostring ());
jsonstring = Sb.tostring ();
Replace "__getzoneresult_ =" so that it can be converted to a Jsonarray object
jsonstring = Jsonstring.replaceall ("^[__]\\w{14}+[_ =]+", "[");
System.out.println (jsonstring+ "]");
String jsonString2 = jsonstring + "]";
Converts a string to a JSON object
Array = Jsonarray.fromobject (jsonString2);

Gets the Jsonobject object of the Jsonarray to easily read the key value pairs in the array
Jsonobject = array.getjsonobject (0);

}catch (Exception e) {
E.printstacktrace ();
}
Return jsonobject.getstring ("province");
}

/**
* To compute the place of ownership of multiple numbers
* @param mobilenumbers Number List
* @return
* @throws malformedurlexception
*/
public static Jsonobject calcmobilescities (list<string> mobilenumbers) throws malformedurlexception{
Jsonobject jsonnumbercity = new Jsonobject ();
for (String mobilenumber:mobilenumbers) {
Jsonnumbercity.put (Mobilenumber, Calcmobilecity (Mobilenumber)); ;
}
return jsonnumbercity;
}

public static void Main (string[] args) throws exception{
String testmobilenumber = "1881758452";
System.out.println (Calcmobilecity (Testmobilenumber));
list<string> mobilelist = new arraylist<string> ();
for (int i = 1350345 i < 1350388; i++) {
Mobilelist.add (string.valueof (i));
}
System.out.println (Calcmobilescities (mobilelist). toString ());
}
}

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.