OKHTTP3 Get post Simple package

Source: Internet
Author: User

Recently intended to use OKHTTP3 in a new project, simply encapsulate the asynchronous get post

Because CallBack is also executed in a child thread, the Handler is used

 Public classmyokhttpclient {Private Staticmyokhttpclient myokhttpclient; Privateokhttpclient okhttpclient; PrivateHandler Handler; Privatemyokhttpclient () {okhttpclient=Newokhttpclient (); Handler=NewHandler (Looper.getmainlooper ()); }     Public Staticmyokhttpclient getinstance () {if(Myokhttpclient = =NULL) {            synchronized(Myokhttpclient.class) {                if(Myokhttpclient = =NULL) {myokhttpclient=Newmyokhttpclient (); }            }        }        returnmyokhttpclient; }     Public voidAsyncget (String URL,Finalhttpcallback Httpcallback) {        FinalRequest Request =Newrequest.builder (). URL (URL). build (); Okhttpclient.newcall (Request). Enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {FinalIOException Fe =e; if(Httpcallback! =NULL) {Handler.post (NewRunnable () {@Override Public voidrun () {httpcallback.onerror (request, FE);                }                    }); }} @Override Public voidOnresponse (call call, Response Response)throwsIOException {FinalString result =response.body (). String (); if(Httpcallback! =NULL) {Handler.post (NewRunnable () {@Override Public voidrun () {httpcallback.onsuccess (request, result);                }                    });    }            }        }); }     Public voidAsyncpost (String URL, formbody formbody,Finalhttpcallback Httpcallback) {        FinalRequest Request =Newrequest.builder (). URL (URL). Post (Formbody). build (); Okhttpclient.newcall (Request). Enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {FinalIOException Fe =e; if(Httpcallback! =NULL) {Handler.post (NewRunnable () {@Override Public voidrun () {httpcallback.onerror (request, FE);                }                    }); }} @Override Public voidOnresponse (call call, Response Response)throwsIOException {FinalString result =response.body (). String (); if(Httpcallback! =NULL) {Handler.post (NewRunnable () {@Override Public voidrun () {httpcallback.onsuccess (request, result);                }                    });    }            }        }); }     Public InterfaceHttpcallback {voidOnError (Request request, IOException E); voidonsuccess (Request request, String result); }}

Use

New Formbody.builder (). Add ("username", "Lesiefang"). Add ("Age", "page"). Build (); Myokhttpclient.getinstance (). Asyncpost (new  myokhttpclient.httpcallback () {    @Override     publicvoid  onError (Request request, IOException e) {        log.d ("AAA", E.tostring ());    }    @Override    publicvoid  onsuccess (Request request, String result) {        Textview.settext (result);}    });

As for uploading files, downloading files, and then wrapping up a similar method.

OKHTTP3 Get post Simple 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.