How Android uses httpclient to get data and add HTTP header information

Source: Internet
Author: User

How to get the data on the HTTP server and append HTTP first class authentication information when getting the data, see the following code:

public void Httpgetdata () {
	try {
		httpclient httpclient = new Defaulthttpclient ();
		String uri = "http://www.yourweb.com"; 
		HttpGet get = new HttpGet (URI);
		Add HTTP header information  
		get.addheader ("Authorization", "Your Token");
		Get.addheader ("Content-type", "Application/json");
		Get.addheader ("User-agent", "Your Agent");
		HttpResponse response;
		Response = Httpclient.execute (get);
		int code = Response.getstatusline (). Getstatuscode ();
		Verify status code if successfully received data if 
		(code = =) {
			//returns JSON format: {"id": "27jpl~j4vsl0lx00e00005", "Version": "ABC"}         
			String rev. = entityutils.tostring (response.getentity ());		
			obj = new Jsonobject (rev);  
			String id = obj.getstring ("id");  
			String Version = obj.getstring ("version");  
		}
	} catch (Exception e) {		
	}
}

Main classes used: Org.apache.http.client.HttpClient, Org.apache.http.client.methods.HttpGet and Org.json.JSONObject

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.