About Android Network Interaction Get,post

Source: Internet
Author: User
Tags http post

The mobile app must have network interaction, whether it is to open the Web page (such as open www.baidu.com on the phone) but with the server-side interaction (access to information, upload or download images, etc.), the use of network requests. Our usual network request is the Get and post in HTTP. Android is very handy for interacting with the Web because Android is primarily developed in the Java language, so it accesses the HTTP service directly using the API under the Java.net package. More conveniently, theAndroid SDK comes with the Apache httpclient API. Apache httpclient is a complete HTTP client that provides full support for the HTTP protocol and can be accessed using HTTP GET and Post methods for easy operation. The following two methods are also briefly described below:

First look at the difference between the Get and post methods (refer to the online information):

(1) Submission method: Get submit, the requested data is written in the URL (that is, the data placed in the HTTP protocol header), to split the URL and transfer data (parameters), multiple parameters with & connection. For example: Login.action?name=hyddd&password=idontknow&gender=male. If the data are all English letters/numbers, sent as is, if there are spaces, converted to +, if there are Chinese/other characters, the string is directly encrypted with BASE64.  Post submission: Place the submitted data (parameters) in the package body of the HTTP packet. As a result, the data submitted by get is displayed in the Address bar, while the post is submitted, the address bar does not change

(2) The size of the transmitted data: the HTTP protocol does not restrict the size of the transmitted data, nor does the HTTP protocol specification limit the URL length. The limitations that exist in real-world development are: get: Specific browsers and servers have a limit on the length of the URL, depending on the support of the operating system. POST: The theoretical data is not limited because it is not transmitted via a URL. However, the actual Web server will be required to limit the size of the post submission data, Apache, IIS6 have their own configuration.

(3) Security: Post security is higher than get security. The meaning of security here is the meaning of real safety, such as: Through get data, user name and password will appear in plaintext on the URL, because (1) the login page is likely to be cached by the browser, (2) Other people to view the browser's history, then others can get your account number and password, In addition, using get to submit data can also cause Cross-site request forgery attack

(4) Method: GET: Request parameter is a sequence of key/value pairs (query string) appended to the URL of the length of the query string is limited by Web browsers and Web servers (such as IE supports up to 2048 characters), not suitable for transmitting large datasets at the same time, It's not safe . Gu ' post: The request parameter is transmitted in a different part of the HTTP header (named entity body), which is used to transfer the form information, so the Content-type must be set to: application/x-www-form- Urlencoded. The post is designed to support user fields on Web Forms, and its parameters are also transmitted as key/value. However: it does not support complex data types, because post does not define the semantics and rules for transferring data structures.

Let's talk about the methods and steps of two methods:

HTTP GET Request steps:

String url= "http://...";//requested network address
The first step is to create the HttpClient object
HttpClient client=new defaulthttpclient ();
Second step, create the HttpGet object
HttpGet httpget = new HttpGet (URL);
The third step is to use the Execute method to send an HTTP GET request and return the HttpResponse object
HttpResponse Response=client.execute (GET);
Fourth step, determine the return status and take out the data returned by the server
if (Httpresponse.getstatusline (). Getstatuscode () = = 200)
{
Fifth step, use the GetEntity method to live to return results
String result = entityutils.tostring (httpresponse.getentity ());
}
HTTP POST request steps:

    String url= "http://...";//requested network address //First step, create HttpClient object
HttpClient client=new defaulthttpclient ();
Second step, create the HttpPost objectHttpPost HttpPost = new HttpPost (URL);// Step three set HTTP POST request parameters must be used with Namevaluepair objectNamevaluepair loginname=new namevaluepair ("LoginName", "User name");
namevaluepair loginpwd=new namevaluepair ("loginpwd", "password");
list<namevaluepair> params = new arraylist<namevaluepair> (); Params.add (loginName); params.add (loginpwd);
//Set HttpPost request parameters httppost.setentity (New urlencodedformentity (params, HTTP. Utf_8));Fourth, use the Execute method to send an HTTP GET request and return the HttpResponse objectHttpResponse response=client.execute (httppost);;if (Httpresponse.getstatusline (). Getstatuscode () = = 200){ Fifth step, use the GetEntity method to live to return resultsString result = entityutils.tostring (httpresponse.getentity ());Finally, the results of the obtained data are processed.

About Android Network Interaction Get,post

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.