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 ;}