我们在创建OkHttp客户端时,可以添加各种拦截器,真的很香:
1 2 3 4 5 6
| OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(new XXXInterceptor()) .addInterceptor(new YYYInterceptor()) .... .build();
|
服务端经常需要我们做一些参数校验的工作,需要在本地先把请求参数封装起来再加密之类的,那么我们可以用拦截器来实现获取所有get或post参数,还可以添加一些公共参数,这样就不用在每个接口定义那去加了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| private static class ParamsInterceptor implements Interceptor { private static final String METHOD_GET = "GET"; private static final String METHOD_POST = "POST"; private static final String HEADER_KEY_USER_AGENT = "User-Agent";
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request.Builder requestBuilder = request.newBuilder(); HttpUrl.Builder urlBuilder = request.url().newBuilder();
if (METHOD_GET.equals(request.method())) { urlBuilder.addEncodedQueryParameter("key_xxx", "value_xxx"); HttpUrl httpUrl = urlBuilder.build(); Set<String> paramKeys = httpUrl.queryParameterNames(); for (String key : paramKeys) { String value = httpUrl.queryParameter(key); Log.d("TEST", key + " " + value); } requestBuilder.url(httpUrl); } else if (METHOD_POST.equals(request.method())) { FormBody.Builder bodyBuilder = new FormBody.Builder(); if (request.body() instanceof FormBody) { FormBody formBody = (FormBody) request.body(); for (int i = 0; i < formBody.size(); i++) { bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i)); } } bodyBuilder.addEncoded("key_xxx", "value_xxx"); FormBody newBody = bodyBuilder.build(); for (int i = 0; i < newBody.size(); i++) { Log.d("TEST", newBody.name(i) + " " + newBody.value(i)); }
requestBuilder.post(newBody); }
requestBuilder.addHeader(HEADER_KEY_USER_AGENT, getUserAgent()); return chain.proceed(requestBuilder.build()); } }
|
为了方便大家参考,我再补充一下import的包,因为这里OkHttp和Retrofit的一些类(比如Response)可能有重名,一定要各自用各自的,千万不要导入错了:
1 2 3 4 5 6
| import okhttp3.FormBody; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;
|
我没有水贴,。