There are two ways in which the client requests the server side in Android: Post mode and get mode
Not directly here, directly on the source code as follows:
(1). How to post:
/** * POST Request mode * * @param model * Request Serial number * @param paramlist * List of data parameters requested by client * @return * / PublicJsonobjectDoPost(intModel, list<namevaluepair> paramlist) {Try{//client sends the requested data to the serverlist<basicnamevaluepair> params =NewArraylist<basicnamevaluepair> (); Params.add (NewBasicnamevaluepair ("param1","China")); Params.add (NewBasicnamevaluepair ("Param2","United States"));//1. Define the way the request object:HttpPost post =NewHttpPost (Base_url);//2. Define the client object for the requestHttpClient client =NewDefaulthttpclient ();//encapsulate the requested data in an entity classhttpentity entity =NewUrlencodedformentity (Paramlist, HTTP. UTF_8);//Place the entity class in the URLPost.setentity (entity);//3. Executing the request, returning the object of the responseHttpResponse response = Client.execute (POST);if(Response! =NULL&& response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get results returned from the server: The result returned from the server is the form of a stringString result = entityutils.tostring (Response.getentity (), HTTP. UTF_8);if(Result.startswith ("{")) {Try{return NewJsonobject (result); }Catch(Jsonexception e) {E.printstacktrace (); } } } }Catch(Clientprotocolexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }return NULL; }
(2). Get mode:
/** * * @param model * Request Serial number * @param paramlist * Client-requested data Parameter list * @return * / PublicJsonobjectDoget(intModel, list<namevaluepair> paramlist) {//client sends the requested data to the server //list<basicnamevaluepair> params = new //Arraylist<basicnamevaluepair> (); //Params.add (New Basicnamevaluepair ("param1", "China"); //Params.add (New Basicnamevaluepair ("param2", "United States"); //pair parameter encodingString param = Urlencodedutils.format (paramlist, HTTP. UTF_8);//url and parameter stitching //HttpGet get = new HttpGet (Base_url + "?" + param);HttpGet get =NewHttpGet (Base_url +"?"+ param); HttpClient HttpClient =NewDefaulthttpclient ();Try{//Initiate a GET requestHttpResponse response = Httpclient.execute (get);if(Response! =NULL&& response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//data that gets the response from the serverString result = entityutils.tostring (Response.getentity (), HTTP. UTF_8);Try{return NewJsonobject (result); }Catch(Jsonexception e) {E.printstacktrace (); } } }Catch(Clientprotocolexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }return NULL; }
The entire tool class code is as follows:
PackageCom.chengdong.su.downloaddemo.service;ImportJava.io.IOException;ImportJava.util.ArrayList;ImportJava.util.List;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.HttpStatus;ImportOrg.apache.http.NameValuePair;ImportOrg.apache.http.client.ClientProtocolException;ImportOrg.apache.http.client.HttpClient;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;ImportOrg.apache.http.client.utils.URLEncodedUtils;ImportOrg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.message.BasicNameValuePair;ImportOrg.apache.http.protocol.HTTP;ImportOrg.apache.http.util.EntityUtils;ImportOrg.json.JSONException;ImportOrg.json.JSONObject;/** * Client requests to server-side tools * * @author SCD * */ Public class httputil { /** The requested URL * / PrivateString Base_url ="Http://www.baidu.com";/** TAG * / PrivateString TAG = GetClass (). Getsimplename ();/** * POST Request mode * * @param model * Request Serial number * @param paramlist * List of data parameters requested by client * @return * / PublicJsonobjectDoPost(intModel, list<namevaluepair> paramlist) {Try{//client sends the requested data to the serverlist<basicnamevaluepair> params =NewArraylist<basicnamevaluepair> (); Params.add (NewBasicnamevaluepair ("param1","China")); Params.add (NewBasicnamevaluepair ("Param2","United States"));//1. Define the way the request object:HttpPost post =NewHttpPost (Base_url);//2. Define the client object for the requestHttpClient client =NewDefaulthttpclient ();//encapsulate the requested data in an entity classhttpentity entity =NewUrlencodedformentity (Paramlist, HTTP. UTF_8);//Place the entity class in the URLPost.setentity (entity);//3. Executing the request, returning the object of the responseHttpResponse response = Client.execute (POST);if(Response! =NULL&& response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get results returned from the server: The result returned from the server is the form of a stringString result = entityutils.tostring (Response.getentity (), HTTP. UTF_8);if(Result.startswith ("{")) {Try{return NewJsonobject (result); }Catch(Jsonexception e) {E.printstacktrace (); } } } }Catch(Clientprotocolexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }return NULL; }/** * * @param model * Request Serial number * @param paramlist * Client-requested data Parameter list * @return * / PublicJsonobjectDoget(intModel, list<namevaluepair> paramlist) {//client sends the requested data to the server //list<basicnamevaluepair> params = new //Arraylist<basicnamevaluepair> (); //Params.add (New Basicnamevaluepair ("param1", "China"); //Params.add (New Basicnamevaluepair ("param2", "United States"); //pair parameter encodingString param = Urlencodedutils.format (paramlist, HTTP. UTF_8);//url and parameter stitching //HttpGet get = new HttpGet (Base_url + "?" + param);HttpGet get =NewHttpGet (Base_url +"?"+ param); HttpClient HttpClient =NewDefaulthttpclient ();Try{//Initiate a GET requestHttpResponse response = Httpclient.execute (get);if(Response! =NULL&& response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//data that gets the response from the serverString result = entityutils.tostring (Response.getentity (), HTTP. UTF_8);Try{return NewJsonobject (result); }Catch(Jsonexception e) {E.printstacktrace (); } } }Catch(Clientprotocolexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }return NULL; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android client Request server-side way to explain (a) attached source