Problems with calling methods for automatic JSON parsing. fromJson () .. readValue (), json. fromjson
The API Store is used to aggregate data and use the mobile phone volume location query function.
Because Baidu's apistore.baidu.com does not accept new user access from January 1, December 2016 to today. An interface for aggregating data is free of charge.
I. through google's google-gson method to parse the aggregated data to get the returned result json code, the jar package used is a gson-2.7.jar.
(1) After Add Build Path, Gson gson = new Gson (); // ctrl + shift + o is normal.
(2) JsonRootBean root = gson. fromJson (result, JsonRootBean. class); // result is the json code returned by aggregate data. The type is string, and JsonRootBean. class is to convert the string to this type.
// Note that the. fromJson () method has many reloads. You must select your own parameters.
(3) You can use the root object to directly call System. out. println ("Company:" + root. getResult (). getCompany () in json ());
Ii. json code returned by jackson method parsing aggregate data, the jar package used is/TestJuHe/lib/jackson-core-2.2.3.jar;/TestJuHe/lib/jackson-annotations-2.2.3.jar;/TestJuHe/lib/jackson-databind-2.2.3.jar
(1) ObjectMapper mapper = new ObjectMapper (); // ctrl + shift + o is normal.
(2) JsonRootBean root = mapper. readValue (result, JsonRootBean. class );
// Note that the. readValue () method has many reloads. You must select your own parameters.
(3) System. out. println ("Company:" + root. getResult (). getCompany (); // use the root object to directly call the content in json
Code for the aggregation data interface:
Import java. io. BufferedReader;
Import java. io. DataOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. UnsupportedEncodingException;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import java.net. URLEncoder;
Import java. util. HashMap;
Import java. util. Map;
Import com. google. gson. Gson;
Import net. sf. json. JSONObject;
/**
* Call the sample code of the mobile phone number in a region-aggregate data
* Online interface documentation: http://www.juhe.cn/docs/11
**/
Public class JuheDemo {
Public static final String DEF_CHATSET = "UTF-8 ";
Public static final int DEF_CONN_TIMEOUT = 30000;
Public static final int DEF_READ_TIMEOUT = 30000;
Public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 ";
// Configure the KEY you applied
Public static final String APPKEY = "c3abbaf3e5b9526fa4959e1edf9716b7"; // This is my key and only valid for the current mobile phone number
// 1. Mobile phone home location query
Public static void getRequest1 (){
String result = null;
String url = "http://apis.juhe.cn/mobile/get"; // request interface address
Map params = new HashMap (); // Request Parameters
Params. put ("phone", "18612626494"); // the first seven digits of the phone number or phone number to be queried
Params. put ("key", APPKEY); // application APPKEY (query on the application details page)
Params. put ("dtype", "json"); // The format of the returned data, in xml or json format. The default value is json.
Try {
// Returned results obtained by applying for aggregation
Result = net (url, params, "GET ");
JSONObject object = JSONObject. fromObject (result );
If (object. getInt ("error_code") = 0 ){
System. out. println (object );
// Manual
// JSONObject json = object. getJSONObject ("result ");
// System. out. println (json. getString ("province "));
// System. out. println (object. getInt ("resultcode "));
// Automatic
// ObjectMapper mapper = new ObjectMapper ();
// JsonRootBean root = mapper. readValue (result, JsonRootBean. class );
// System. out. println (root );
// System. out. println ("Company:" + root. getResult (). getCompany ());
// Use the gson Method
Gson gson = new Gson ();
JsonRootBean root = gson. fromJson (result, JsonRootBean. class );
System. out. println ("Company:" + root. getResult (). getCompany ());
} Else {
System. out. println (object. get ("error_code") + ":" + object. get ("reason "));
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
GetRequest1 ();
}
/**
*
* @ Param strUrl: request address
* @ Param params Request Parameters
* @ Param method Request method
* @ Return network request string
* @ Throws Exception
*/
Public static String net (String strUrl, Map params, String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
Try {
StringBuffer sb = new StringBuffer ();
If (method = null | method. equals ("GET ")){
StrUrl = strUrl + "? "+ Urlencode (params );
}
URL url = new URL (strUrl );
Conn = (HttpURLConnection) url. openConnection ();
If (method = null | method. equals ("GET ")){
Conn. setRequestMethod ("GET ");
} Else {
Conn. setRequestMethod ("POST ");
Conn. setDoOutput (true );
}
Conn. setRequestProperty ("User-agent", userAgent );
Conn. setUseCaches (false );
Conn. setConnectTimeout (DEF_CONN_TIMEOUT );
Conn. setReadTimeout (DEF_READ_TIMEOUT );
Conn. setInstanceFollowRedirects (false );
Conn. connect ();
If (params! = Null & method. equals ("POST ")){
Try {
DataOutputStream out = new DataOutputStream (conn. getOutputStream ());
Out. writeBytes (urlencode (params ));
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
}
InputStream is = conn. getInputStream ();
Reader = new BufferedReader (new InputStreamReader (is, DEF_CHATSET ));
String strRead = null;
While (strRead = reader. readLine ())! = Null ){
Sb. append (strRead );
}
Rs = sb. toString ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
If (reader! = Null ){
Reader. close ();
}
If (conn! = Null ){
Conn. disconnect ();
}
}
Return rs;
}
// Convert map type to request parameter type
Public static String urlencode (Map <String, String> data ){
StringBuilder sb = new StringBuilder ();
For (Map. Entry I: data. entrySet ()){
Try {
Sb. append (I. getKey ()). append ("= "). append (URLEncoder. encode (I. getValue () + "", "UTF-8 ")). append ("&");
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
}
}
Return sb. toString ();
}
}