[英]Retrofit 2.0 multiple interceptors
我正在进行改造,需要能够使用多个拦截器。 目前我正在使用一个自动附加身份验证令牌,但我需要能够在没有身份验证令牌的情况下进行呼叫。 如果我在标题中添加另一个没有auth令牌的拦截器,我该如何使用那个而不是auth令牌拦截器。
val interceptor: Interceptor = Interceptor { chain ->
val newRequest = chain.request().newBuilder().
addHeader("Auth_Token", pref.getString(PSPreferences.prefAuthKey, "")).
cacheControl(CacheControl.FORCE_NETWORK).
build()
chain.proceed(newRequest)
}
okHttpClient = OkHttpClient.Builder().
readTimeout(1, TimeUnit.MINUTES).
connectTimeout(1, TimeUnit.MINUTES).
addInterceptor(interceptor).build()
val retrofitInstance = Retrofit.Builder()
.baseUrl(APIEndpointInterface.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
apiInterface = retrofitInstance.create<APIEndpointInterface>(APIEndpointInterface::class.java)
OkHttpClient
维护一个可以访问的拦截器列表,但它是一个不可修改的集合。
这让我们有三个选择:
创建两个OkHttpClient实例,并扣除两个Retrofit实例,一个用于未经身份验证的请求,另一个用于经过身份验证的请求。
检查是否应该使用拦截器,例如在您的身份验证拦截器中,您可以首先检查您的令牌首选项中是否存在密钥,如果是,请使用它; 如果没有,你只需继续而不修改任何东西。 您也可以为未经验证的拦截器执行此操作。 我认为这是您案件最简单的解决方案。
创建一个拦截器,它将维护一个可修改的拦截器列表,您可以随意添加和删除它们。 您需要保留对此拦截器的引用,可能使其成为Singleton。
对于第三种选择,我提供了一个非常简单的例子:
public class HttpRequestResponseInterceptor implements Interceptor {
public final List<RequestInterceptor> requestInterceptors = new ArrayList<>();
public final List<ResponseInterceptor> responseInterceptors = new ArrayList<>();
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
for (RequestInterceptor interceptor : requestInterceptors) {
request = interceptor.intercept(request);
}
Response response = chain.proceed(request);
for (ResponseInterceptor interceptor : responseInterceptors) {
response = interceptor.intercept(response);
}
return response;
}
public interface RequestInterceptor {
Request intercept(Request request) throws IOException;
}
public interface ResponseInterceptor {
Response intercept(Response response) throws IOException;
}
}
在这种情况下,您需要实现自定义接口RequestInterceptor
和ResponseInterceptor
。
这些接口的实现示例如下:
public class ExampleInterceptor implements HttpRequestResponseInterceptor.RequestInterceptor,
HttpRequestResponseInterceptor.ResponseInterceptor {
@Override
public Request intercept(Request request) throws IOException {
return request.newBuilder().addHeader("REQUEST_HEADER", "EXAMPLE").build();
}
@Override
public Response intercept(Response response) throws IOException {
return response.newBuilder().addHeader("RESPONSE_HEADER", "EXAMPLE").build();
}
}
然后,您需要将此拦截器添加到我们的主拦截器两次,一次是requestInterceptors
,一次是responseInterceptors
(如果它只拦截请求或仅响应,则只添加到其中一个)。
这个例子远非完整。 此解决方案的好处是它添加了添加和删除拦截器的功能,而无需重新创建OkHttpClient
实例。 例如,如果要支持重试请求,则需要额外的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.