OKHttp简单封装

2020-11-26  本文已影响0人  小李同学今天博学了吗

接口:

package com.example.mvptest;

public interface ReqCallBack<T> {
    void onRequSuccess(T result);

    void onReqFailed(String errorMsg);
}

管理类

package com.example.mvptest;

import android.content.Context;
import android.os.Handler;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpManager {
    private static volatile  OkHttpManager mInstancee;
    private OkHttpClient okHttpClient;
    private Handler okHttpHandler;

    private OkHttpManager(Context context){
           okHttpClient = new OkHttpClient();
           okHttpHandler = new Handler(context.getMainLooper());
    }

    public static OkHttpManager getInstance(Context context){
        if(mInstancee == null){
            synchronized (OkHttpManager.class){
                if(mInstancee == null){
                    mInstancee = new OkHttpManager(context);
                }
            }
        }
        return mInstancee;
    }

    public <T> Call getAsycHttp(String url, final ReqCallBack<T> callBack){
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                failedCallBack("访问失败",callBack);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if(response.isSuccessful()){
                    String string = response.body().string();
                    successCallBack((T)string,callBack);
                }else{
                    failedCallBack("服务器错误",callBack);
                }
            }
        });

        return call;
    }

    private <T> void successCallBack(final T result, final ReqCallBack<T> callBack){
            okHttpHandler.post(new Runnable() {
                @Override
                public void run() {
                    if(callBack != null){
                        callBack.onRequSuccess(result);
                    }
                }
            });
    }

    private <T> void failedCallBack(final String errorMsg, final ReqCallBack<T> callBack){
           okHttpHandler.post(new Runnable() {
               @Override
               public void run() {
                   if(callBack != null){
                       callBack.onReqFailed(errorMsg);
                   }
               }
           });
    }

}

使用

 OkHttpManager.getInstance(this).getAsycHttp("https://www.baidu.com", new ReqCallBack<String>() {
            @Override
            public void onRequSuccess(String result) {
                Log.e(TAG, "onRequSuccess: "+result);
            }

            @Override
            public void onReqFailed(String errorMsg) {

            }
        });
上一篇下一篇

猜你喜欢

热点阅读