Android Client POST request server-side instance _android

Source: Internet
Author: User

Android client Request server-side detailed explanation

1. Android client and server-side communication methods:
Android communicates with the server in the form of HTTP communication and socket communication, while HTTP communication methods are two ways of Get and post.
2. Parse server-side return data interpretation:
(1). For the server side, the data format that is 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 that, in contrast to the XML data Interchange format, because parsing XML is more complex and requires writing large sections of code, So the data interchange format between client and server is often exchanged via JSON.
3. Android, use Get and post to access HTTP resources
(1). When a client sends a request to the server, it transmits a block of data to the server, which is the request information.
(2). Get and Post differences:
A:get requests that the submitted data be placed in the HTTP request protocol header (that is, the URL), and that post-submitted data is placed in the Entity data, with high security.
The B:get method can have up to 1024 bytes of data submitted, and Post does not have this limit.

Note: Considering the advantages of post, in Android development you think it is best to use post, so you write a small post request example. 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; /*** * Client Invocation method: Pass parameter to send request to server * * @param userId
   * @param userName * @return/public static Jsonobject GetData (String userId, String userName) {int mode
    LId = user_id;
    list<namevaluepair> list = new arraylist<namevaluepair> ();
    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<namevaluepair> paramlist)
    {///1. Create Request object HttpPost HttpPost = new HttpPost (Inner_url);
    Post request mode data is placed 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 client object HttpClient httpclient = new Defaulthttpclient (); 3. The client requests a server-side try {//server-side return request data HttpResponse Httpre with the Request objectSponse = Httpclient.execute (HttpPost);
        Resolves 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.