Use of Okhttp

Source: Internet
Author: User

Simply say the reason for learning okhttp

    1. Google removed the HttpClient API from Android 6.0, using the Okhttp
    2. Efficient use of HTTP to make applications run faster and with less traffic
    3. Respond to cached data to avoid duplicate network requests
    4. Seamless support for gzip to reduce data traffic
    5. Using a very simple, Request-and-response API with smooth construction and immutability, while supporting synchronous asynchronous invocation of callback functions
    6. If there is a problem with the network, it recovers from common connectivity problems
    7. 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

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.