封装OKHttpMap集合直接解析、拦截器、单例模式

一、拦截器

 //拦截器
    private static Interceptor getAppInterceptor(){
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Log.i("aaa","-----------"+"拦截前");
                Response response = chain.proceed(request);
                Log.i("aaa","-----------"+"拦截后");
                return response;
            }
        };
        return interceptor;
    }

二、单例模式

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

三、OKHttpMap集合get和post请求

//封装get请求
    public  void doGet(String url, final Class clazz, final NetCallBack netCallBack){
        OkHttpClient build = new OkHttpClient().newBuilder().addInterceptor(getAppInterceptor()).build();
        Request request = new Request.Builder().url(url).get().build();
        build.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                netCallBack.onFailed(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String s = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        netCallBack.onSuccess(s);
                    }
                });
            }
        });
    }
    //封装post请求
    public  static void doPost(String url, Map<String,String> map, final Class clazz, final NetCallBack netCallBack){
        //不是FormBody,而是一个Builder
        FormBody.Builder body = new FormBody.Builder();
        //key   value
        for (String key:map.keySet()){
            //value的值
            body.add(key,map.get(key));
        }
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(getAppInterceptor()).build();
        Request request = new Request.Builder().url(url).post(body.build()).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                netCallBack.onFailed(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String s = response.body().string();
                Log.i("sss", "run: "+s.toString());
                final Object o = new Gson().fromJson(s, clazz);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        netCallBack.onSuccess(o);
                    }
                });
            }
        });

    }


    //创建一个接口
    public interface NetCallBack{
        void onSuccess(Object o);
        void onFailed(Exception e);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值