How to handle HTTP GET and POST requests using the Okhttp package in Android _android

Source: Internet
Author: User
Tags connection pooling

Overview
HTTP is now the mainstream application of the network request method, used to exchange data and content, effective use of HTTP can make your app faster and reduce the use of traffic
Okhttp is a great HTTP client (GitHub home: https://github.com/square/okhttp):

    • Supports spdy that can merge multiple requests to the same host
    • Use connection pooling technology to reduce request latency (if Spdy is available)
    • Use gzip compression to reduce the amount of data transferred
    • Caching responses to avoid duplicate network requests

When your network is crowded, it is okhttp, it can avoid common network problems, if your service is deployed on different IP, if the first connection fails, Okhttp will try other connections.  This is now common in Ipv4+ipv6 to deploy service redundancy to different data centers. Okhttp will use the current TLS attribute (SNI ALPN) to initialize the new connection. If the handshake fails, switch to SLLv3

It is easy to use okhttp, while supporting asynchronous blocking requests and callbacks.
If you use Okhttp, you don't have to rewrite your code, the Okhttp-urlconnection module implements the API in Java.net.HttpURLConnection, The Okhttp-apache module implements the API in HttpClient


Get Request
starting with the simplest Http request, we first need to get a Okhttpclient object, as follows:

Okhttpclient mhttpclient = new Okhttpclient ();

Okhttpclient is used to manage all requests and internally supports concurrency, so we don't have to create a Okhttpclient object every time it's requested, which is very resource intensive. The next step is to create a Request object, as follows:

Request Request = new Request.builder ()
  . URL ("http://www.baidu.com")
  . Build ();

Then call this request:

Mokhttpclient.newcall (Request). Enqueue (New Callback () {
  @Override public
  void OnFailure IOException e) {
  }
  @Override public
  void Onresponse (call call, Response Response) throws IOException {
    LOGGER.T ("123123"). I (Response.body (). String ());
  }
});

The method requires an incoming callback interface to respond to a successful and failed callback by calling the Newcall method to pass in the request object that was created before, and then calling the Enqueue () method to begin the asynchronous request.
Okay, that's it. Run the program and print the following data in Logcat:

Yes, this is Baidu returned the HTML code, we look very laborious, but the browser is based on these code to parse, and then the Baidu home page display.
OK, the simplest HTTP request has been completed, and there are probably three steps to this function:
Creating Okhttpclient Objects
Create a Request object
Add the Request object to the Okhttpclient and pass in the callback function.


POST Request
we all know that HTTP commonly used requests are divided into get and POST requests, which we used is obviously a GET request, then, how to initiate a POST request? In fact it is very simple, in the second step there is a post method, you can pass in the request parameters.
First, create a Requestbody object, where we create a form data parameter using one of its subclasses formbody, as follows:

form data
Formbody.builder Builder = new Formbody.builder ();
Builder.add ("Xwdoor", "Xwdoor");
Requestbody formbody = Builder.build ();

The Request object is then constructed as follows:

Request Request = new Request.builder ()
  . URL ("http:www.baidu.com")
  . Post (formbody). Build
  ();

Call the Post () method and pass in the created Requestbody object. The start request is the same as the previous GET request. There are approximately four steps to implementing a POST request:

    • Creating Okhttpclient Objects
    • Creating Requestbody Objects
    • Create a Request object

Add the Request object to the Okhttpclient and pass in the callback function.

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.