OKHttp原码分析(四)之getResponseWithInterceptorChain方法
来源:互联网 发布:国外翻译的c语言算法书 编辑:程序博客网 时间:2024/05/21 08:44
一,概述
在OKHttp原码分析(一)最后讲到无论是同步请求还是异步请求都殊途同归到了RealCall的getResponseWithInterceptorChain方法。这篇blog主要讲解RealCall的getResponseWithInterceptorChain方法。
二,getResponseWithInterceptorChain方法源码
private Response getResponseWithInterceptorChain() throws IOException { // Build a full stack of interceptors. List<Interceptor> interceptors = new ArrayList<>(); 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 (!retryAndFollowUpInterceptor.isForWebSocket()) { interceptors.addAll(client.networkInterceptors()); } interceptors.add(new CallServerInterceptor( retryAndFollowUpInterceptor.isForWebSocket())); Interceptor.Chain chain = new RealInterceptorChain( interceptors, null, null, null, 0, originalRequest); return chain.proceed(originalRequest); }
注意点:
- 方法的返回值是Response 。这个就是网络请求的目的,得到的数据都封装在Response 对象中。
- 拦截器的使用。在方法的第一行中就创建了interceptors 集合,然后紧接着放进去很多拦截器对象。
- RealInterceptorChain类的proceed方法。getResponseWithInterceptorChain方法的最后创建了RealInterceptorChain对象,并调用proceed方法。Response 对象就有由RealInterceptorChain类的proceed方法返回的。
下面看RealInterceptorChain类的proceed方法的原码。
三,RealInterceptorChain类的proceed方法的原码
public Response proceed(Request request, StreamAllocation streamAllocation, HttpStream httpStream,Connection connection) throws IOException { if (index >= interceptors.size()) throw new AssertionError(); calls++; // If we already have a stream, confirm that this is the only call to chain.proceed(). if (this.httpStream != null && calls > 1) { throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must call proceed() exactly once"); } // Call the next interceptor in the chain. RealInterceptorChain next = new RealInterceptorChain( interceptors, streamAllocation, httpStream, connection, index + 1, request); Interceptor interceptor = interceptors.get(index); Response response = interceptor.intercept(next); return response; }
分析:
- 注意index和calls两个整形字段,如果index大于等于集合的长度,calls就++,calls大于1就会抛出异常。所以在正常情况下index是小于集合的长度的。
- index的初始化在RealInterceptorChain的构造方法中,也就是在getResponseWithInterceptorChain放中创建RealInterceptorChain对象时被初始化的。我们发现初始化值是0。
- 在该方法末尾再次创建RealInterceptorChain对象,此时index值是1。然后得到拦截器对象,调用拦截器的intercept方法,并把index等于1的RealInterceptorChain对象传递过去。
- interceptor的intercept方法的返回值是Response 。注意Response 是从这儿来的。
0 0
- OKHttp原码分析(四)之getResponseWithInterceptorChain方法
- OKHttp原码分析(五)之Interceptor
- OKHttp原码分析(六)之RealConnection
- OKHttp原码分析(七)之HttpStream
- OKHttp原码分析(一)
- OKHttp原码分析(八)之必须明白的几个问题
- OkHttp源码分析(四)DiskLruCache
- OkHttp之BridgeInterceptor简单分析
- Okhttp之CacheInterceptor简单分析
- OkHttp之ConnectInterceptor简单分析
- OkHttp实现分析之Websocket
- Okhttp之CallServerInterceptor简单分析
- OkHttp实现分析之Websocket
- webrtc原码分析
- recommenderjob原码分析
- atoi原码分析
- OKHTTP源码分析(一)异步方法
- OkHttp源码分析之基本框架1
- Fibonacci数列的递归与非递归实现
- Spring学习五:Bean自动装配
- leetcode100same tree
- Android Icon尺寸标准
- JS创建对象之动态原型模式
- OKHttp原码分析(四)之getResponseWithInterceptorChain方法
- Struts2中 Result的值,加不加斜杠的区别
- 多媒体
- 读书笔记《Effective C++》条款08:别让异常逃离析构函数
- powerDesign 把name自动添加到Commment里
- 文件操作指令
- ubuntu搭建arm-linux-gcc
- 配置使用freeline入门
- kodova混合模式手机开发禁止横屏问题:如何实现和新浪微博一样的横屏?