Explanation of client request server in Android (1) with source code

Source: Internet
Author: User

Explanation of client request server in Android (1) with source code

In Android, client requests are sent to the server in two ways: Post and Get.
The source code is as follows:
(1). Post method:

/*** Post Request Method ** @ param model * request serial number * @ param paramList * List of data parameters requested by the client * @ return */public JSONObject doPost (int model, List
  
   
ParamList) {try {// List of data requested by the client to the server
   
    
Params = new ArrayList
    
     
(); Params. add (new BasicNameValuePair (param1, China); params. add (new BasicNameValuePair (param2, USA); // 1. define the Request Method object: HttpPost post = new HttpPost (Base_URL); // 2. define the requested client object HttpClient client = new DefaultHttpClient (); // encapsulate the requested data in the entity class HttpEntity entity = new UrlEncodedFormEntity (paramList, HTTP. UTF_8); // place the object class in the URL post. setEntity (entity); // 3. execute the request and return the response object HttpResponse response = client.exe cute (po St); if (response! = Null & response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// get the result returned from the server: The result returned from the server is in the String format String result = EntityUtils. toString (response. getEntity (), HTTP. UTF_8); if (result. startsWith ({) {try {return new JSONObject (result);} catch (JSONException e) {e. printStackTrace () ;}}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;}
    
   
  

(2). Get method:

/***** @ Param model * Request No. * @ param paramList * List of data parameters requested by the client * @ return */public JSONObject doGet (int model, List
  
   
ParamList) {// data requested by the client to the server // List
   
    
Params = new // ArrayList
    
     
(); // Params. add (new BasicNameValuePair (param1, China); // params. add (new BasicNameValuePair (param2, USA); // encode the parameter String param = URLEncodedUtils. format (paramList, HTTP. UTF_8); // concatenate the URL and parameters // HttpGet get = new HttpGet (Base_URL +? + Param); HttpGet get = new HttpGet (Base_URL +? + Param); HttpClient httpClient = new DefaultHttpClient (); try {// initiate the GET request HttpResponse response = httpClient.exe cute (get); if (response! = Null & response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// obtain the response data from the Server String result = EntityUtils. toString (response. getEntity (), HTTP. UTF_8); try {return new JSONObject (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:

Package com. chengdong. su. downloaddemo. service; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. httpStatus; import org. apache. http. nameValuePair; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpGet; import org. apache. http. client. methods. httpPost; import org. apache. http. client. utils. URLEncodedUtils; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; import org. json. JSONException; import org. json. JSONObject;/*** client-side request tool ** @ author scd **/public class HttpUtil {/** request URL */private String Base_URL = http://www.baidu.com; /** TAG */private String TAG = getClass (). getSimpleName (); /*** Post Request Method ** @ param model * request serial number * @ param paramList * List of data parameters requested by the client * @ return */public JSONObject doPost (int model, List
  
   
ParamList) {try {// List of data requested by the client to the server
   
    
Params = new ArrayList
    
     
(); Params. add (new BasicNameValuePair (param1, China); params. add (new BasicNameValuePair (param2, USA); // 1. define the Request Method object: HttpPost post = new HttpPost (Base_URL); // 2. define the requested client object HttpClient client = new DefaultHttpClient (); // encapsulate the requested data in the entity class HttpEntity entity = new UrlEncodedFormEntity (paramList, HTTP. UTF_8); // place the object class in the URL post. setEntity (entity); // 3. execute the request and return the response object HttpResponse response = client.exe cute (po St); if (response! = Null & response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// get the result returned from the server: The result returned from the server is in the String format String result = EntityUtils. toString (response. getEntity (), HTTP. UTF_8); if (result. startsWith ({) {try {return new JSONObject (result);} catch (JSONException e) {e. printStackTrace () ;}}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;} /***** @ param model * Request No. * @ param paramList * List of data parameters requested by the client * @ return */public JSONObject doGet (int model, List
     
      
ParamList) {// data requested by the client to the server // List
      
        Params = new // ArrayList
       
         (); // Params. add (new BasicNameValuePair (param1, China); // params. add (new BasicNameValuePair (param2, USA); // encode the parameter String param = URLEncodedUtils. format (paramList, HTTP. UTF_8); // concatenate the URL and parameters // HttpGet get = new HttpGet (Base_URL +? + Param); HttpGet get = new HttpGet (Base_URL +? + Param); HttpClient httpClient = new DefaultHttpClient (); try {// initiate the GET request HttpResponse response = httpClient.exe cute (get); if (response! = Null & response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {// obtain the response data from the Server String result = EntityUtils. toString (response. getEntity (), HTTP. UTF_8); try {return new JSONObject (result);} catch (JSONException e) {e. printStackTrace () ;}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;}}
       
      
     
    
   
  

 

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.