拦截器的作用在APP开发中是相当重要的,这篇文章分析一下Okhttp中是怎么实现一条拦截器链,希望能帮助大家更好去了解拦截器的作用
几个关键类以及接口
-
RealInterceptorChain
-
Interceptor
RealInterceptorChain
RealInterceptorChain实现了Interceptor接口,调用源头来自于RealCall的getResponseWithInterceptorChain方法
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
/**
* 1
*/
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
/**
* 2
*/
Interceptor.Chain chain = new RealInterceptorChain(
interceptors, null, null, null, 0, originalRequest);
return chain.proceed(originalRequest);
}
1. 第一处是把自定义的拦截器先加入进去,然后把okhttp内置的一些拦截器按照功能顺序add进list里
2. 第二处是整个拦截器工作的源头,先new了一个RealInterceptorChain,index为0,把初始的request传了进去,然后调用proceed获取请求的response
接下来看一下proceed方法的具体实现,忽略掉一些检查
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
if (index >= interceptors.size()) throw new AssertionError();
calls++;
/**
* 1
*/
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
return response;
}
先是检查一下index是否超出interceptors也就是拦截器list的长度,接着又new了一个RealInterceptorChain,把当前RealInterceptorChain的一些参数属性都传了进去,并且在这里把index + 1,这个地方很重要,在这里可以把RealInterceptorChain想象成把拦截器给包装了起来,然后用下一个拦截器生成一个新的RealInterceptorChain中,然后把index+1下标的chain传进了当前拦截器的intercept方法里,也就是我们自定义拦截器的时候需要重写的intercept方法,也就是说我们添加的拦截器里的intercept方法中RealInterceptorChain对象,实际上里面包含有整个request以及下一个拦截器。所以就形成了一条链,每一个拦截器都持有下一个拦截器经过包装之后的RealInterceptorChain对象,直到拦截器链的最后一个拦截器CallServerInterceptor 这个拦截器会做最后的处理,然后开始请求生成Response并且返回。拦截器可以通过intercept方法获取下一个拦截器return的response,而且会通过proceed方法向下一个拦截器传递自己处理过的Requeset
到这里总结一下各个方法以及类的作用
-
RealInterceptorChain
包装了拦截器以及网络请求的信息
-
RealInterceptorChain.proceed
参数中有
Request,是上一个拦截器包装过后的Request,然后将下一个拦截器和整个Request的信息包装成RealInterceptorChain,并且会调用当前拦截器的intercept方法把下一个拦截器(RealInterceptorChain)传进去,并且获取到Response
-
Interceptor.intercept
方法参数是下一个
Chain也就是RealInterceptorChain,然后调用RealInterceptorChain.request()去获取请求,调用RealInterceptorChain.proceed()去获取Response