自定义拦截器

这次要做的是通过自定义拦截器为接口设置公共的参数:不用我们再一一添加公共参数
想使用这个参数之前要先导入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 paramMap;
    //有参构造
    public PublicParamInterceptor(Map 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 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 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 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 map = new HashMap<>();
        map.put("公共参数key", "公共参数value");
        PublicParamInterceptor publicParamInterceptor = new PublicParamInterceptor(map);

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

你可能感兴趣的:(网络)