自定义拦截器

这次要做的是通过自定义拦截器为接口设置公共的参数:不用我们再一一添加公共参数
想使用这个参数之前要先导入okHttp的依赖

implementation 'com.squareup.okhttp3:okhttp:3.12.0'

自定义拦截器代码:

/**
 * date:2017/6/28
 * author:辉(家辉辉辉)
 * function:自定义拦截器:功能封装公共的请求参数
 * implements Interceptor使用它要先导入依赖
 */
public class PublicParamInterceptor implements Interceptor {
    //初始化Map对象
    private Map<String,String> paramMap;
    //有参构造
    public PublicParamInterceptor(Map<String,String> paramMap) {
        this.paramMap = paramMap;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        //拿到原来的request
        Request oldRequest = chain.request();
        //拿到请求的url
        String url = oldRequest.url().toString();
        //判断是GET还有POST请求
        if(oldRequest.method().equalsIgnoreCase("GET")){
            if(paramMap != null && paramMap.size() > 0){
                StringBuilder builder = new StringBuilder(url);
                //拼接公共请求参数
                for(Map.Entry<String,String> entry: paramMap.entrySet()){
                    builder.append("&" + entry.getKey() + "=" + entry.getValue());
                }
                //将拼接好的网址赋值给url
                url = builder.toString();
                //如果之前的url没有?号,我们需要手动给它添加一个?号
                if(!url.contains("?")){
                    url = url.replaceFirst("&", "?");
                }
                //依据原来的request构造一个新的request
                Request request = oldRequest.newBuilder()
                        .url(url)
                        .build();
                return chain.proceed(request);
            }
        }else{//post
            if(paramMap != null && paramMap.size() > 0){
                RequestBody body = oldRequest.body();

                //body instanceof FormBody是判断formBody是否是body的类
                if(body != null && body instanceof FormBody){
                    FormBody formBody = (FormBody) body;
                    //1.把原来的body里面的参数添加到新的body中
                    FormBody.Builder builder = new FormBody.Builder();
                    //为了防止重复添加相同的key和value
                    HashMap<String, String> temMap = new HashMap<>();
                    for(int i=0 ; i < formBody.size() ; i++){
                        builder.add(formBody.encodedName(i),formBody.encodedValue(i));
                        temMap.put(formBody.encodedName(i),formBody.encodedValue(i));
                    }
                    //2.把公共请求参数添加到新的body中
                    for(Map.Entry<String,String> entry : paramMap.entrySet()){
                        if(!temMap.containsKey(entry.getKey())){
                            builder.add(entry.getKey(),entry.getValue());
                        }
                    }
                    FormBody newFormBody = builder.build();
                    //依据原来的request构造一个新的request
                    Request newRequest = oldRequest.newBuilder()
                            .post(newFormBody)
                            .build();
                    return chain.proceed(newRequest);
                }
            }

        }

        return chain.proceed(oldRequest);
    }
}

在工具类中的调用

      //okhttp添加公共参数到拦截器中
        Map<String, String> map = new HashMap<>();
        map.put("公共参数key", "公共参数value");
        PublicParamInterceptor publicParamInterceptor = new PublicParamInterceptor(map);

	mOkHttpClien = new OkHttpClient.Builder()
               ...
                //添加okhttp的拦截器
                .addInterceptor(publicParamInterceptor)
               ..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值