Simply say the reason for learning okhttp
- Google removed the HttpClient API from Android 6.0, using the Okhttp
- Efficient use of HTTP to make applications run faster and with less traffic
- Respond to cached data to avoid duplicate network requests
- Seamless support for gzip to reduce data traffic
- Using a very simple, Request-and-response API with smooth construction and immutability, while supporting synchronous asynchronous invocation of callback functions
- If there is a problem with the network, it recovers from common connectivity problems
- If a server is configured with multiple IP addresses, when the first IP connection fails, it tries to connect to the next IP
The above is Okhttp official website http://square.github.io/okhttp/and combine some of the online understanding to organize
Configuring the Environment
Add in Builde.gradle
' com.squareup.okhttp3:okhttp:3.4.1 '
Add the required permissions in Androidmanifest.xml
<uses-permission android:name= "android.permission.INTERNET"/>
Basic use
Import com.google.gson.Gson;
Import com.google.gson.JsonParseException;
Import com.google.gson.JsonSyntaxException;
Import java.io.IOException;
Import java.util.Map;
Import java.util.concurrent.TimeUnit;
Import okhttp3. call;
Import okhttp3. Callback;
Import okhttp3. formbody;
Import okhttp3. okhttpclient;
Import okhttp3. Request;
Import okhttp3. requestbody;
Import okhttp3. Response;
/**
* Created by 95224 on 2016.
*/
public class Okhttphelper {
private static Okhttpclient okhttpclient;
Private Gson mgson;
Private Okhttphelper () {
Okhttpclient =new okhttphelper.builder (). connecttimeout (timeunit.seconds). writetimeout (10,TimeUnit.SECONDS)
. ReadTimeout (20,timeunit.seconds)
. Build ();
Mgson = new Gson ();
}
public static Okhttphelper getinstance () {
return new Okhttphelper ();
}
public void DoRequest (final request request, final Basecallback Callback) {
Callback.onrequestbrfore (request);
Okhttpclient.newcall (request) enqueue (new Callback () {
@Override
public void OnFailure (call call, ioexception E) {
Callback.onfailure (request,e);
}
@Override
public void Onresponse (call call, Response Response) throws IOException {
If (response.issuccessful ()) {
String Resultstr=response.body (). string ();
if (callback.mtype = = String.class) {
Callback.onsuccess (response,resultstr);
}else{
try {
Object object = Mgson.fromjson (resultstr, callback.mtype);
Callback.onsuccess (response, object);
}catch (jsonsyntaxexception E) {
Callback.onerror (response,response.code (), e);
}catch (jsonparseexception E) {
Callback.onerror (response,response.code (), e);
}
}
}else{
Callback.onerror (response,response.code (), null);
}
}
});
}
public void Get (String url,basecallback callback) {
Request Request = Buildrequest (url, null, httpmethodtype.get);
DoRequest (request,callback);
}
public void post (String url, map<string, object> params,basecallback Callback) {
Request Request =buildrequest (url,params, httpmethodtype.post);
DoRequest (request,callback);
}
Private Request buildrequest (String url, map<string, object> params, httpmethodtype Methodtype) {
Request.builder Builder = new Request.builder ();
Builder.url (url);
if (methodtype = = Httpmethodtype.get) {
Builder.get ();
} else if (methodtype = = Httpmethodtype.post) {
Requestbody BODY = Buildformatdata (params);
Builder.post (body);
}
return Builder.build ();
}
Private Requestbody buildformatdata (map<string, object> Params) {
Formbody.builder builder=new Formbody.builder ();
If (params!=null) {
For (map.entry<string,object> Entry:params.entrySet ()) {
Builder.add (entry.getkey (), entry.getvalue (). toString ());
}
}
return Builder.build ();
}
Enum httpmethodtype{
Get,post
}
Use of Okhttp