Frame: https://github.com/square/okhttp
Today to explain the use of Network framework Okhttp, this framework is very powerful, many frameworks use it to load network resources, many developers are still using asynchttp, the library is now deprecated, Google has removed the httpclient, Let's take a look at the use of okhttp.
First in the Build.gradle file, add the library and synchronize, if the eclipse user will download the JAR package Import Libs can
Compile ' com.squareup.okhttp3:okhttp:3.4.1 '
Okhttpclient httpClient = new Okhttpclient ();
Request Request = new Request.builder (). URL (weather_url). build ();
New Thread () {
@Override
public void Run () {
Super.run ();
try {
Response Response = httpclient.newcall (Request). Execute ();
Final String BODY = Response.body (). String ();
Runonuithread (New Runnable () {
@Override
public void Run () {
Setview (body);
}
});
} catch (IOException e) {
E.printstacktrace ();
}
}
}.start ();
Basic use or relatively concise
Instance a Okhttp Object
Construct a request parameter where you can construct the settings for the various request parameters, such as Url,head,method, etc.
Get and post are also reflected here, the above is a GET request, the default is null, we all know, look at the source
Public Builder get () {
Return method ("GET", null);
}
Note that there is a need to turn on thread send requests, return response, and then update the UI on the main thread
You can also use callback call for this request, but be aware that this is just a child-thread asynchronous callback, which means that the update UI still uses handler or the Runonuithread method.
Httpclient.newcall (Request) Enqueue (new Callback () {
@Override
public void OnFailure (call call, IOException e) {
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
Final String BODY = Response.body (). String ();
Runonuithread (New Runnable () {
@Override
public void Run () {
Setview (body);
}
});
}
});
The above is the use of GET requests, if it is a POST request, carrying parameters, only the request construction is different, the other, the following is the method:
Formbody.builder Builder = new Formbody.builder ();
Builder.add ("username", "Dongcai");
Builder.add ("E_Mail", "[email protected]");
Formbody build = Builder.build ();
Final Request request = new Request.builder (). URL (weather_url). Post (build). Build ();
Here to illustrate, the post to the parameter is Requestbody, which is an abstract class, inside the package supports a variety of data forms, etc., very powerful. We found the implementation subclass of it.
Public final class Formbody extends Requestbody
Formbody this encapsulates the method of requesting a parameter collection, you need to use the Builder class to add the request parameters, and then construct the Formbody object to send the POST request. You can also set the encoding method here
Public Builder Add (string name, String value) {
Names.add (httpurl.canonicalize (name, Form_encode_set, False, False, True, true));
Values.add (Httpurl.canonicalize (value, Form_encode_set, False, False, True, true));
return this;
}
Public Builder addencoded (string name, String value) {
Names.add (httpurl.canonicalize (name, Form_encode_set, True, False, True, true));
Values.add (Httpurl.canonicalize (value, Form_encode_set, True, False, True, true));
return this;
}
The above is my reference document and source summary under the basic method of use, relative to Asynchttp, volley, I am afraid the biggest problem is to update ui,okhttp is not very convenient, the message back to write the main thread. However, Okhttp supports SPDY, can consolidate multiple to the same host, use connection pooling technology to reduce the latency of requests (if Spdy is available), use gzip compression to reduce the amount of data transferred, cache responses to avoid duplicate network requests, interceptors, and so on.
Use of Okhttp in Android