OkHttp encapsulation and okhttp Encapsulation

Source: Internet
Author: User

OkHttp encapsulation and okhttp Encapsulation

OkHttp Encapsulation

Private static OkHttpUtils okHttpUtils = null; private MyHandler myHandler = new MyHandler (); private OkLoadListener okLoadListener; // Single Instance public static OkHttpUtils getInstance () {if (okHttpUtils = null) {okHttpUtils = new OkHttpUtils ();} return okHttpUtils;} // get public void okGet (String url) {OkHttpClient client = new OkHttpClient. builder (). addInterceptor (new MyInter ()). build (); Request request = new Request. builder (). url (url ). build (); Call call = client. newCall (request); call. enqueue (new Callback () {@ Override public void onFailure (Call call, IOException e) {Message message = myHandler. obtainMessage (); message. what = 0; message. obj = e. getMessage (); myHandler. sendMessage (message) ;}@ Override public void onResponse (Call call, Response response) throws IOException {Message message = myHandler. obtainMessage (); message. what = 1; message. obj = response. body (). string (); myHandler. sendMessage (message) ;}}) ;}// post public void okPost (String url, Map
 
  
Map) {OkHttpClient client = new OkHttpClient. builder (). addInterceptor (new MyInter ()). build (); // create FormBody. builder builder = new FormBody. builder (); // traverse map Set
  
   
Keys = map. keySet (); for (String key: keys) {String value = map. get (key); builder. add (key, value + "") ;}// build FormBody body = builder. build (); Request request = new Request. builder (). url (url ). post (body ). build (); Call call = client. newCall (request); call. enqueue (new Callback () {@ Override public void onFailure (Call call, IOException e) {Message message = myHandler. obtainMessage (); message. what = 0; message. obj = e. getMessage (); myHandler. sendMessage (message) ;}@ Override public void onResponse (Call call, Response response) throws IOException {Message message = myHandler. obtainMessage (); message. what = 1; message. obj = response. body (). string (); myHandler. sendMessage (message) ;}}) ;}// Interceptor class MyInter implements Interceptor {private static final String TAG = "MyInter"; @ Override public Response intercept (Chain chain Chain) throws IOException {// obtain the original body Request = chain. request (); RequestBody body = request. body (); if (body instanceof FormBody) {// traverse all the original parameters, add them to the new Body, and add the public parameters to the new Body FormBody. builder newBuilder = new FormBody. builder (); for (int I = 0; I <(FormBody) body ). size (); I ++) {String name = (FormBody) body ). name (I); String value = (FormBody) body ). value (I); // Add it to the new newBuilder. add (name, value);} // add public parameters to newBuilder. add ("source", "android"); FormBody newBody = newBuilder. build (); // create a new Request request newRequest = Request. newBuilder (). post (newBody ). build (); Response response = chain. proceed (newRequest); return response;} return null ;}// handler class MyHandler extends Handler {@ Override public void handleMessage (Message msg) {switch (msg. what) {case 0: // failed String e = (String) msg. obj; okLoadListener. okLoadError (e); break; case 1: // success String json = (String) msg. obj; okLoadListener. okLoadSuccess (json); break ;}}// improve the public void setOkLoadListener (OkLoadListener okLoadListener) of the external call interface {this. okLoadListener = okLoadListener ;}
  
 

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.