处理OkHttp的异常

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43218083/article/details/85133874

OkUtil:

public class OkUtil {
    private static OkUtil instance;
    private OkHttpClient mClient;
    private Handler handler=new Handler(Looper.getMainLooper());
    public static OkUtil getInstance(){
        if (instance==null){
            synchronized (OkUtil.class){
                instance=new OkUtil();
            }
        }
        return instance;
    }
    private OkUtil(){
        //拦截器
        CustomIn interceptor=new CustomIn();
        /*interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);*/
        mClient=new OkHttpClient.Builder()
                .readTimeout(10,TimeUnit.SECONDS)
                .connectTimeout(10,TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();
    }
    public void postenqueue(String url, Map<String,String> parmas, final Class clazz, final ICallBack callBack){
        FormBody.Builder builder=new FormBody.Builder();
        //迭代器
        for (Map.Entry<String,String> entry : parmas.entrySet()){
            builder.add(entry.getKey(),entry.getValue());
        }
        RequestBody body=builder.build();
        Request request=new Request.Builder()
                .post(body)
                .url(url)
                .build();
        Call call=mClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.falied(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                final Object o = new Gson().fromJson(result, clazz);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.success(o);
                    }
                });
            }
        });
    }
    public void gettenqueue(String url, Map<String,String> parmas, final Class clazz, final ICallBack callBack){
        Request request=new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call=mClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.falied(e);
            }

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

                try {
                    String result = response.body().string();
                    final Object o = new Gson().fromJson(result, clazz);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.success(o);
                        }
                    });
                }catch(Exception e){
                    callBack.falied(e);
                }

            }
        });
    }
}

展开阅读全文

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