OkHttp3的简单封装复制可用

废话前言

之前使用okhttp都是用一次new一次相关对象,也只要封装一下,但是没有心思去封装,也不太会。现在看到这位兄弟的简单封装,实为恍然大悟。

导包

    compile 'com.squareup.okhttp3:okhttp:3.10.0'
    compile 'com.squareup.okio:okio:1.11.0'

封装代码

这个已经高于我的层次了,看了有的也封装了GSON,但是我水平不够,看不懂,就用这个了

public class OkHttpClientManager {

    private static int TimeOut = 120;

    //单例获取ohttp3对象

    private static OkHttpClient okHttp = null;

    public static synchronized OkHttpClient getInstance() {

        if (okHttp == null) {

            okHttp = new OkHttpClient.Builder()

                    .readTimeout(TimeOut, TimeUnit.SECONDS)

                    .connectTimeout(TimeOut, TimeUnit.SECONDS)

                    .writeTimeout(TimeOut, TimeUnit.SECONDS)

                    .build();

        }

        return okHttp;

    }


    /**
     * 自定义callback
     */

    static class MyCallBack implements Callback {

        private OkHttpCallBack okHttpCallBack;

        public MyCallBack(OkHttpCallBack okHttpCallBack) {

            this.okHttpCallBack = okHttpCallBack;

        }

        @Override
        public void onFailure(Call call, IOException e) {
            okHttpCallBack.onFailure(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            okHttpCallBack.onSuccess(response);
        }
    }


    /**
     * 接口方法
     */

    public interface OkHttpCallBack {

        void onFailure(IOException e);

        void onSuccess(Response response);

    }


    /**
     * post请求
     *
     * @param url
     * @param map
     * @param callback
     */

    public static void doPost(String url, Map map, OkHttpCallBack callback) {

        MyCallBack myCallback = new MyCallBack(callback);

        FormBody.Builder formBody = new FormBody.Builder();

        //解析转化map

        if (null != map && map.size() > 0) {

            for (Map.Entry entry : map.entrySet()) {

                formBody.add(entry.getKey(), entry.getValue().toString());

            }

        }

        RequestBody requestBody = formBody.build();

        Request request = new Request.Builder()

                .url(url)

                .post(requestBody)

                .build();

        okHttp.newCall(request).enqueue(myCallback);


    }


    /**
     * get请求
     *
     * @param url
     * @param callback
     */

    public static void doGet(String url, OkHttpCallBack callback) {

        MyCallBack myCallback = new MyCallBack(callback);

        Request request = new Request.Builder()

                .url(url)

                .get()

                .build();

        okHttp.newCall(request).enqueue(myCallback);

    }

}

复制直接可用

Activity调用

private void okHttpGet() {

        OkHttpClientManager.getInstance();
        String url = "http://gc.ditu.aliyun.com/regeocoding?l=39.938133,116.395739&type=001";

        OkHttpClientManager.doGet(url, new OkHttpClientManager.OkHttpCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onSuccess(final Response response) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            result.setText(response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

    }

就这样了,就可以了,这次记录,也只有get、post封装了方法,以后补充其他的功能。
有水平了,就把GSON封装进去。谢谢这位兄弟的简单封装

你可能感兴趣的:(android)