1. 程式人生 > >OkHttp封裝好的工具類

OkHttp封裝好的工具類

public class OkHttpUtil {

    private final Handler mHandler;
    private final OkHttpClient mOkHttpClient;
    private static OkHttpUtil sOkHttpUtil;

    private OkHttpUtil(){

        //攔截器
        Interceptor interceptor = new Interceptor();

        //如果建立Handler的物件,是在一個普通的類裡建立,那麼一定要加上Looper.getMainLooper()這個引數
        mHandler = new Handler(Looper.getMainLooper());
        mOkHttpClient = new OkHttpClient.Builder()
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .addInterceptor(interceptor)
                .build();
    }

    //單例暴露一個普通的方法,給對方,雙重鎖模式
    public static OkHttpUtil getInstance(){
        if (sOkHttpUtil  ==null){
            synchronized (OkHttpUtil.class){
                if (sOkHttpUtil == null){
                    return sOkHttpUtil =new OkHttpUtil();
                }
            }
        }
        return sOkHttpUtil;
    }

    //介面
    public interface OkCallback {
        void onFailure(Exception e);
        void onResponse(String json);
    }

    //okhttp與handler
    //封裝doGEt的網路封裝,引數定義兩個,一個是URL網址   一個實現介面的物件
    public void doGet(String url , final OkCallback mOkCallback){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (mOkCallback !=null){
                    //切換到主執行緒
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mOkCallback.onFailure(e);
                        }
                    });
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (mOkCallback !=null){
                        //切換到主執行緒
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mOkCallback.onResponse(json);
                            }
                        });
                    }
                }

            }
        });
    }

    //封裝doPost的邏輯程式碼
    public void doPost(String url, Map<String,String> map, final OkCallback mOkCallback){
        //建立FormBody物件,把表單新增到FormBody
        FormBody.Builder builder = new FormBody.Builder();
        //集合物件不為null的情況下
        if (map != null){
            for(String key: map.keySet()){
                builder.add(key,map.get(key));
            }
        }
        FormBody formBody = builder.build();
        //建立Request物件
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (mOkCallback !=null){
                    //切換到主執行緒
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mOkCallback.onFailure(e);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (mOkCallback !=null){
                        //切換到主執行緒
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mOkCallback.onResponse(json);
                            }
                        });
                    }
                }
            }
        });

    }
}