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 ());
}
}