Detailed explanation of the Android client Request server side

Source: Internet
Author: User

Detailed explanation of the Android client Request server side

1. Android client and server-side communication mode:
Android and server communication usually uses the HTTP communication mode and socket communication mode, and the HTTP communication method is divided into two ways of Get and post.
2. Parsing the server-side return data interpretation:
(1). For the server side, 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 interchange format, because parsing XML is more complex than XML, and requires writing large pieces of code. Therefore, the data Interchange format of client and server is often exchanged through JSON.
3. In Android, Access HTTP resources with Get and post
(1). When a client sends a request to the server, a data block is sent to the server, which is the request information.
(2). Get and Post differences:
A: Get requests submit data placed in the HTTP request protocol header (that is, the URL), while the data submitted by the post is placed in the Entity data, security is relatively high.
B: The Get method submits only 1024 bytes of data, while Post does not have this limit.
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 example of a POST request. The code is as follows:

 PackageCom.scd.jsondemo.util;ImportJava.io.IOException;ImportJava.io.UnsupportedEncodingException;ImportJava.util.ArrayList;ImportJava.util.List;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpResponse;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.HttpPost;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; Public  class jsonutil {    /** Address * /    Private Static FinalString Inner_url ="http://localhost:8080/index2.jsp";/** TAG * /    Private FinalString TAG = GetClass (). Getsimplename ();Private Static Final intUSER_ID =1;/*** * Client Invoke method: Pass parameter send request to server * * @param userId * @param userName * @return      */     Public StaticJsonobjectGetData(String userId, String userName) {intModelID = user_id; list<namevaluepair> list =NewArraylist<namevaluepair> (); List.add (NewBasicnamevaluepair ("UserId", userId)); List.add (NewBasicnamevaluepair ("UserName", UserName));returnDoPost (ModelID, list); }/** * Method of Requesting Server * * @param model * @param paramlist * @return */    Private StaticJsonobjectDoPost(intModel, list<namevaluepair> paramlist) {//1. Create a Request objectHttpPost HttpPost =NewHttpPost (Inner_url);//Post request method data is placed in the entity classhttpentity entity =NULL;Try{entity =NewUrlencodedformentity (Paramlist, HTTP.            UTF_8);        Httppost.setentity (entity); }Catch(Unsupportedencodingexception E1)        {E1.printstacktrace (); }//2. Creating Client ObjectsHttpClient HttpClient =NewDefaulthttpclient ();//3. The client requests the server side with the request object        Try{//server-side returns the requested dataHttpResponse HttpResponse = Httpclient.execute (HttpPost);//Parse the data returned by the request            if(HttpResponse! =NULL&& httpresponse.getstatusline (). Getstatuscode () = = $) {String element = entityutils.tostring (Httpresponse.getentity (), HTTP. UTF_8);if(Element.startswith ("{")) {Try{return NewJsonobject (Element); }Catch(Jsonexception e)                    {E.printstacktrace (); }                }            }        }Catch(Clientprotocolexception e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }return NULL; }}

Detailed explanation of the Android client Request server side

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.