OKHttp网络请求工具包

依赖

implementation 'com.squareup.okhttp3:okhttp:3.11.0'
 implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'

OkHttpClients 工具类

public class OkHttpClients {

    private final OkHttpClient okHttpClient;

    public OkHttpClients() {
        //日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .callTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
    }

    public static OkHttpClients getInstance(){
        return OkHttpHoulder.utils;
    }
    static class OkHttpHoulder{
        private static final OkHttpClients utils=new OkHttpClients();
    }

    //封装同步的Get请求方式
    public String getSync(String urlStr) throws IOException {
        Request request = new Request.Builder().url(urlStr).build();
        return okHttpClient.newCall(request).execute().body().string();
    }

    //同步的Post
    public String postAync(String urlStr,String key,String value) throws IOException {
        RequestBody body =new FormBody.Builder().add(key,value).build();
        Request request = new Request.Builder().url(urlStr).post(body).build();
        return okHttpClient.newCall(request).execute().body().string();
    }

    //异步的get和post

    public void getAsync(String url, Callback callback) {
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public void postAsync(String url, Callback callback) {
        RequestBody body = new FormBody.Builder().add("key", "value").build();
        Request request = new Request.Builder().url(url).post(body).build();
        okHttpClient.newCall(request).enqueue(callback);
    }
}

还有一种是MVP中直接使用OKHttp的写法

public class ModelImpl implements Model{
    private MyCallBack myCallBack;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                HomeBean bean = (HomeBean) msg.obj;
                myCallBack.startSuccess(bean);
                myCallBack.startError(bean);
            }else{
                MyBean myBean = (MyBean) msg.obj;
                myCallBack.startSuccess(myBean);
                myCallBack.startError(myBean);
            }

        }
    };

    @Override
    public void start(final String urls, MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
        if(urls=="http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4"){
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder().url(urls).build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Reader reader = response.body().charStream();
                    HomeBean bean = new Gson().fromJson(reader, HomeBean.class);
                    handler.sendMessage(handler.obtainMessage(0,bean));
                }
            });
        }else{
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder().url(urls).build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    Reader reader = response.body().charStream();
                    MyBean bean = new Gson().fromJson(reader, MyBean.class);
                    handler.sendMessage(handler.obtainMessage(1,bean));
                }
            });
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值