Okhttp for Android Package

Source: Internet
Author: User

1. Encapsulates the Get method, handler update the main thread, callback Onsuccess,onfailure,onerror and other methods

2. Configuration files

' com.android.support:recyclerview-v7:26.1.0 '  'com.squareup.okhttp3:okhttp:3.9.1'//fastjson' com.alibaba:fastjson:1.2.44 '

3. First set up an HTTP package, build Basehttpcallback and Okhttphelper These 2 classes, will add loading ... This thing

 Public classOkhttphelper {Private Staticokhttpclient okhttpclient; PrivateAndroid.os.Handler Handler; PrivateOkhttphelper () {okhttpclient=Newokhttpclient (); Handler=NewAndroid.os.Handler (Looper.getmainlooper ());    };  Public StaticOkhttphelper getinstance () {return NewOkhttphelper (); }     Public void Get(String url,basehttpcallback basehttpcallback) {Request Request= Buildrequest (URL,NULL, Httpmethodtype.get);    DoRequest (Request, basehttpcallback); }     Public voidPost (String URL, map<string,string>params, Basehttpcallback Basehttpcallback) {Request Request= Buildrequest (URL,NULL, Httpmethodtype.post);    DoRequest (Request, basehttpcallback); }     Public voiddorequest (Final request request, final Basehttpcallback basehttpcallback) {Basehttpcallback.onrequestbefore (re        Quest); Okhttpclient.newcall (Request). Enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {callbackfailure (basehttpcallback,e); } @Override Public voidOnresponse (call call, Response Response) throws IOException {if(Response.issuccessful ()) {String s= Response.body ().string();                Callbacksuccess (basehttpcallback,response,s); }Else{callbackerror (Basehttpcallback,response,response.code ());    }            }        }); }    PrivateRequest buildrequest (String url,map<string,string>params, Httpmethodtype Methodtype) {Request.builder Builder=NewRequest.builder ();        Builder.url (URL); if(methodtype==httpmethodtype.get) {Builder.Get(); }Else if(methodtype==httpmethodtype.post) {        }        returnBuilder.build (); }    enumhttpmethodtype{GET, POST}Private voidcallbacksuccess (Final Basehttpcallback callback, final Response Response, final String s) {handler.post (NewRunnable () {@Override Public voidrun () {callback.onsuccess (response,s);    }        }); }    Private voidCallbackerror (Final Basehttpcallback callback, final Response Response, finalintcode) {Handler.post (NewRunnable () {@Override Public voidrun () {callback.onerror (Response,code,NULL);    }        }); }    Private voidcallbackfailure (Final Basehttpcallback callback, final Exception e) {handler.post (NewRunnable () {@Override Public voidrun () {callback.onfailure (e);    }        }); }}
 Public Abstract classBasehttpcallback { Public Abstract voidOnrequestbefore (Request request);  Public Abstract voidOnsuccess (Response response,stringstring);  Public Abstract voidOnError (Response Response,intcode,exception e);  Public Abstract voidonfailure (Exception e);}

4. A different user class is also encapsulated

 Public classUser { PublicString result;  PublicList<data>data;  Public classdata{ PublicString name;  PublicString age;  PublicString GetName () {returnname; }         PublicString getage () {returnAge ; }    }     PublicString GetResult () {returnresult; }     PublicList<data>Getdatas () {returndata; }}

Example of 5.activity call

Okhttphelper Httphelper =okhttphelper.getinstance (); String URL="http://www.baidu.com"; Httphelper.Get(URL,NewBasehttpcallback () {@Override Public voidOnrequestbefore (Request request) {} @Override Public voidOnsuccess (Response Response, Stringstring) {} @Override Public voidOnError (Response Response,intcode, Exception e) {} @Override Public voidonfailure (Exception e) {toast.maketext (mainactivity. This,"the current network is not available, please check the network", Toast.length_long). Show (); }        });

6. Imperfect, no post, no loading effect,

Okhttp for Android Package

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.