Android server operations, android server operations

Source: Internet
Author: User

Android server operations, android server operations

Simple and Convenient

/*** @ Author think * Send an Http request synchronously */public class ApacheHttpClient {/*** @ return **/public String httpGet (String uri) {String response = null; // response HttpClient httpClient = new DefaultHttpClient (); // create an HttpGet object HttpGet httpGet = new HttpGet (uri); HttpResponse httpResponse; try {// use the execute method to send an HTTP getrequest and return the httpresponseobject httpresponse=httpclient.exe cute (httpGet); int statusCode = httpResponse. getStatusLine (). getStatusCode (); // return code, if (statusCode = HttpStatus. SC _ OK) {// get the returned result response = EntityUtils. toString (httpResponse. getEntity ();} else {response = "return code:" + statusCode;} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (response); return response;}/*** send the request in Post mode * @ param url request address * @ param params parameter, the Post method must use the NameValuePair [] Array Storage parameter * @ return * @ throws Exception */public String httpPost (String uri, List <NameValuePair> params) throws Exception {String response = null; httpClient httpClient = new DefaultHttpClient (); HttpPost httpPost = new HttpPost (uri); try {// set the httpPost request parameter httpPost. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); // use the execute method to send an HTTP Post request and return the HttpResponse object HttpResponse httpResponse=httpClient.exe cute (httpPost); int statusCode = httpResponse. getStatusLine (). getStatusCode (); // return code, if (statusCode = HttpStatus. SC _ OK) {response = EntityUtils. toString (httpResponse. getEntity (); System. out. println ("______________" + response);} else {response = "return code:" + statusCode; System. out. println ("______________" + response) ;}} catch (Exception e) {e. printStackTrace ();} return response ;}}


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.