最新的OkhttpUtils改进版

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sui_yz/article/details/80112692
package com.bwie.cuihaokai.net;

import android.os.Handler;
import android.os.Looper;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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

public class OkhttpUtils {
    //单例模式 懒汉式
    private static OkhttpUtils okhttpUtils;
    private final OkHttpClient client;
    private final Handler handler;

    private OkhttpUtils() {
        client = new OkHttpClient.Builder()
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .connectTimeout(20, TimeUnit.SECONDS)
                .build();
        handler = new Handler(Looper.getMainLooper());
    }
    //获得一个实例
    public static OkhttpUtils getInstance() {
        if (okhttpUtils == null) {
            //加同步锁安全
            synchronized (OkhttpUtils.class) {
                if (okhttpUtils == null) {
                    okhttpUtils = new OkhttpUtils();
                }
            }
        }
        return okhttpUtils;
    }

    //get请求
    public void doGet(String url, final OnNetListener onNetListener) {
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                onNetListener.onFailed(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(string);
                    }
                });
            }
        });
    }

    //post请求方法
    public void doPost(String url, Map<String, String> params, final OnNetListener onNetListener) {
        if (params == null) {
            return;
        }
        //创建Requeset对象
        FormBody.Builder builder = new FormBody.Builder();
        //添加参数
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        //请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                onNetListener.onFailed(e);
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {

                //拿到服务器返回的json字符串
                final String string = response.body().string();

                //把回调放到主线程里
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(string);
                    }
                });

//                onNetListener.onSuccess(string);
            }
        });
    }
}

展开阅读全文

没有更多推荐了,返回首页