For details about how to post data in json format, add an http header, and accept the returned data, see the following code:
View plaincopy to clipboardprint? Private void HttpPostData (){
Try {
HttpClient httpclient = new DefaultHttpClient ();
String uri = "http://www.yourweb.com ";
HttpPost httppost = new HttpPost (uri );
// Add http header information
Httppost. addHeader ("Authorization", "your token"); // authenticate token
Httppost. addHeader ("Content-Type", "application/json ");
Httppost. addHeader ("User-Agent", "imgfornote ");
// Json data format of http post: {"name": "your name", "parentId": "id_of_parent "}
JSONObject obj = new JSONObject ();
Obj. put ("name", "your name ");
Obj. put ("parentId", "your parentid ");
Httppost. setEntity (new StringEntity (obj. toString ()));
HttpResponse response;
Response = httpclient.exe cute (httppost );
// Check the status code. If the data is successfully received
Int code = response. getStatusLine (). getStatusCode ();
If (code = 200 ){
String rev = EntityUtils. toString (response. getEntity (); // return json format: {"id": "27JpL ~ J4vsL0LX00E00005 "," version ":" abc "}
Obj = new JSONObject (rev );
String id = obj. getString ("id ");
String version = obj. getString ("version ");
}
} Catch (ClientProtocolException e ){
} Catch (IOException e ){
} Catch (Exception e ){
}
}
Private void HttpPostData (){
Try {
HttpClient httpclient = new DefaultHttpClient ();
String uri = "http://www.yourweb.com ";
HttpPost httppost = new HttpPost (uri );
// Add http header information
Httppost. addHeader ("Authorization", "your token"); // authenticate token
Httppost. addHeader ("Content-Type", "application/json ");
Httppost. addHeader ("User-Agent", "imgfornote ");
// Json data format of http post: {"name": "your name", "parentId": "id_of_parent "}
JSONObject obj = new JSONObject ();
Obj. put ("name", "your name ");
Obj. put ("parentId", "your parentid ");
Httppost. setEntity (new StringEntity (obj. toString ()));
HttpResponse response;
Response = httpclient.exe cute (httppost );
// Check the status code. If the data is successfully received
Int code = response. getStatusLine (). getStatusCode ();
If (code = 200 ){
String rev = EntityUtils. toString (response. getEntity (); // return json format: {"id": "27JpL ~ J4vsL0LX00E00005 "," version ":" abc "}
Obj = new JSONObject (rev );
String id = obj. getString ("id ");
String version = obj. getString ("version ");
}
} Catch (ClientProtocolException e ){
} Catch (IOException e ){
} Catch (Exception e ){
}
} Main classes used: org. apache. http. client. HttpClient, org. apache. http. client. methods. HttpPost and org. json. JSONObject
Author's "spiritual pure column"