OKHttp 执行拦截器前
2018-01-11 14:38
204 查看
在同步和异步方法中都调用
作用:将创建系统拦截器和读取我们定义的拦截器 放入拦截器链中 并执行 proceed(....)
作用: 创建新拦截器链执行 拦截器intercept方法
图:
getResponseWithInterceptorChain():
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 (!forWebSocket) {//默认是false interceptors.addAll(client.networkInterceptors()); //由我们能定义的 } interceptors.add(new CallServerInterceptor(forWebSocket)); //创建拦截器链 并调用proceed(...) Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest, this, eventListener, client.connectTimeoutMillis(), client.readTimeoutMillis(), client.writeTimeoutMillis()); return chain.proceed(originalRequest); }
作用:将创建系统拦截器和读取我们定义的拦截器 放入拦截器链中 并执行 proceed(....)
@Override public Response proceed(Request request) throws IOException { return proceed(request, streamAllocation, httpCodec, connection); //重载方法 } public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec, RealConnection connection) throws IOException { if (index >= interceptors.size()) throw new AssertionError(); calls++; // If we already have a stream, confirm that the incoming request will use it. if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) { throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must retain the same host and port"); } // If we already have a stream, confirm that this is the only call to chain.proceed(). if (this.httpCodec != null && calls > 1) { throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must call proceed() exactly once"); } // Call the next interceptor in the chain. // index+1 下次进来时取得下一个拦截器 RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec, connection, index + 1, request, call, eventListener, connectTimeout, readTimeout, writeTimeout); //取出拦截器并执行intercept(...)方法 Interceptor interceptor = interceptors.get(index); Response response = interceptor.intercept(next); // Confirm that the next interceptor made its required call to chain.proceed(). 确保每个拦截器都调用了proceed if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) { throw new IllegalStateException("network interceptor " + interceptor + " must call proceed() exactly once"); } // Confirm that the intercepted response isn't null. if (response == null) { throw new NullPointerException("interceptor " + interceptor + " returned null"); } if (response.body() == null) { throw new IllegalStateException( "interceptor " + interceptor + " returned a response with no body"); } return response; }
作用: 创建新拦截器链执行 拦截器intercept方法
图:
相关文章推荐
- Struts2中拦截器与过滤器的执行顺序
- OkHttp3 拦截器 封装公共参数
- 常用拦截器的执行步骤
- 拦截器(如登录控制拦截器)什么时候执行,比如输入http://127.0.0.1:8080/testProject,同一个url执行几次?
- 使用MVP注册登录模块+封装的OKhttp,拦截器+QQ第三方登录+RecyclerView+SpringView上拉加载下拉刷新网络数据
- OKHTTP拦截器工作过程
- 通过拦截器来统计每个action的执行时间
- SpringMVC拦截器执行过程源码分析
- 解决方法|struts2执行action前加入拦截器,导致获取不到参数
- 过滤器 拦截器 controller 页面 的执行顺序
- OkHttp的基本使用-3(添加拦截器和GET请求的参数拼接)
- Okhttp源码解析之Interceptor(拦截器)
- 给retrofit 的 添加okhttp的拦截器,可以让她显示 请求和返回的信息 ,便于查找错误
- OKhttp源码解析---拦截器之CacheInterceptor
- springmvc中中央控制器、处理器、过滤器和拦截器的执行时机
- shiro过滤器,web.xml中默认访问页面,springMVC拦截器执行顺序
- OkHttp拦截器之获取Response.body的内容
- 过滤器 拦截器 service 执行顺序
- Struts2 拦截器的执行顺序(二十九)
- 拦截器与过滤器的区别以及他们的执行顺序