OKHttpUtils的封装(get post)

杨运泽勿看
public class OkHttpUtils {


    private final Handler handler;
    private final OkHttpClient okHttpClient;
    private static OkHttpUtils okHttpUtils;

    private OkHttpUtils() {
        //拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        handler = new Handler(Looper.getMainLooper());
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .build();
    }

    //设置单例
    public static OkHttpUtils getInstance() {
        if (okHttpUtils == null) {
            synchronized (OkHttpUtils.class) {
                if (okHttpUtils == null) {
                    return okHttpUtils = new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }

    //设置get请求
    public void doGet(String url, final onCallBack onCallBack) {

        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (onCallBack != null) {
                    onCallBack.onFaild(e);
                }
            }

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

                try {
                    if (response != null && response.isSuccessful()) {
                        final String json = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (onCallBack != null) {
                                    onCallBack.onResponse(json);
                                }
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (onCallBack != null) {
                    onCallBack.onFaild(new Exception("有异常"));
                }
            }
        });


    }


    //设置post请求

    public void doPost(String url, Map map, final onCallBack onCallBack) {

        FormBody.Builder builder = new FormBody.Builder();
        for (String key : map.keySet()) {
            builder.add(key, map.get(key));
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (onCallBack != null) {
                    onCallBack.onFaild(e);
                }
            }

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

                try {
                    if (response != null && response.isSuccessful()) {
                        final String json = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (onCallBack != null) {
                                    onCallBack.onResponse(json);
                                }
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (onCallBack != null) {
                    onCallBack.onFaild(new Exception("有异常"));
                }
            }
        });
    }

    //设置接口
    public interface onCallBack {
        void onFaild(Exception e);

        void onResponse(String json);
    }

}

你可能感兴趣的:(OKHttpUtils的封装(get post))