系列文章
OKHttp源码解析(2)----拦截器RetryAndFollowUpInterceptor
OKHttp源码解析(3)----拦截器BridgeInterceptor
OKHttp源码解析(4)----拦截器CacheInterceptor
OKHttp源码解析(5)----拦截器ConnectInterceptor
OKHttp源码解析(6)----拦截器CallServerInterceptor
1.简介
Opens a connection to the target server and proceeds to the next interceptor.
连接拦截器,负责和服务器建立连接
2.源码解析
@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
StreamAllocation streamAllocation = realChain.streamAllocation();
// We need the network to satisfy this request. Possibly for validating a conditional GET.
boolean doExtensiveHealthChecks = !request.method().equals("GET");
//在连接池中找个可用的链接,并创建HttpCodec
HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
RealConnection connection = streamAllocation.connection();
return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
复制代码
连接拦截器的代码比较简单,主要是将网络设置组合起来,给下一个拦截器----CallServerInterceptor,用于向服务器发起真正的网络请求。这些网络设施主要有request(请求)、streamAllocation(网络设施,可以获取HttpCodec和RealConnection)、HttpCodec(htt编解码器)和RealConnection(连接)
实际的网络请求可以在拦截器CallServerInterceptor查看。
小结
未完待续。