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
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 ;}}