Android client request Server

Source: Internet
Author: User

Android client request Server

Android client request Server

1. Communication between the Android client and the server:
Android and server usually use HTTP and Socket communication methods, while HTTP Communication methods are divided into get and post methods.
2. Explanation for parsing data returned by the server:
(1 ).For the server, the data format returned to the client is generally divided into three formats: html, xml, and json.
(2 ).JSON (Javascript Object Notation) is a lightweight data exchange format. Compared with the xml data exchange format, parsing xml is complicated and requires writing large sections of code, therefore, the data exchange format between the client and the server is usually in JSON format.
3. Use GET and POST to access http resources in Android
(1 ).When the client sends a request to the server, it sends a data block, that is, the request information, to the server.
(2 ).Difference between GET and POST:
A:The data submitted for the GET request is placed in the HTTP Request Header (that is, the url), and the data submitted for the POST is placed in the object data, which is highly secure.
B:Data submitted in GET mode can contain a maximum of 1024 bytes, whereas POST mode does not.

Note: considering the advantages of POST, I think it is best to use the POST Request Method in Android development. So I wrote a small POST request example below. The Code is as follows:

Package com. scd. jsondemo. util; import java. io. IOException; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpEntity; import org. apache. http. httpResponse; 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. httpPost; 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; public class JsonUtil {/** address */private static final String INNER_URL = "http: // localhost: 8080/index2.jsp "; /** TAG */private final String TAG = getClass (). getSimpleName (); private static final int USER_ID = 1;/***** method called by the client: send a request ** @ param userId * @ param userName * @ return */public static JSONObject getData (String userId, String userName) {int modelId = USER_ID; List
  
   
List = new ArrayList
   
    
(); List. add (new BasicNameValuePair ("userId", userId); list. add (new BasicNameValuePair ("userName", userName); return doPost (modelId, list );} /*** request server method ** @ param model * @ param paramList * @ return */private static JSONObject doPost (int model, List
    
     
ParamList) {// 1. create a request object HttpPost httpPost = new HttpPost (INNER_URL); // put the post Request Method Data in the entity class HttpEntity entity = null; try {entity = new UrlEncodedFormEntity (paramList, HTTP. UTF_8); httpPost. setEntity (entity);} catch (UnsupportedEncodingException e1) {e1.printStackTrace ();} // 2. create the client object HttpClient httpClient = new DefaultHttpClient (); // 3. the client sends a request object to the server. try {// The server returns the requested data. HttpResponse httpResp Onse = httpClient.exe cute (httpPost); // parse the data returned by the request if (httpResponse! = Null & httpResponse. getStatusLine (). getStatusCode () == 200) {String element = EntityUtils. toString (httpResponse. getEntity (), HTTP. UTF_8); if (element. startsWith ("{") {try {return new JSONObject (element);} catch (JSONException e) {e. printStackTrace () ;}}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;}}
    
   
  

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.