How does Android use HttpClient to submit data in Post mode and add http header information?

Source: Internet
Author: User

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"

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.