- Response response = getResponseWithInterceptorChain();
- if (retryAndFollowUpInterceptor.isCanceled()) {
- signalledCallback = true;
- responseCallback.onFailure(RealCall.this, new IOException(“Canceled”));
- } else {
- signalledCallback = true;
- responseCallback.onResponse(RealCall.this, response);
- }
- } catch (IOException e) {
- if (signalledCallback) {
- // Do not signal the callback twice!
- Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
- } else {
- responseCallback.onFailure(RealCall.this, e);
- }
- } finally {
- client.dispatcher().finished(this);
- }
- }
- }
execute方法的逻辑并不复杂,简单的说就是:
调用getResponseWithInterceptorChain获取服务器返回
通知任务分发器(client.dispatcher)该任务已结束
getResponseWithInterceptorChain构建了一个拦截器链,通过依次执行该拦截器链中的每一个拦截器最终得到服务器返回。
3. 构建拦截器链
首先来看下getResponseWithInterceptorChain的实现:
源码路径:okhttp3/RealCall.java
// 开始执行整个请求
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
// 拦截器栈
List interceptors = new ArrayList<>();
// 前文说过的 普通拦截器
interceptors.addAll(client.interceptors());
// 重试拦截器,网络错误、请求失败等
interceptors.add(retryAndFollowUpInterceptor);
// 桥接拦截器,主要是重构请求头即header
interceptors.add(new BridgeInterceptor(client.cookieJar()));
// 缓存拦截器
interceptors.add(newCacheInterceptor(client.internalCache()));
// 连接拦截器,连接服务器,https包装
interceptors.add(new ConnectInterceptor(client));
// 网络拦截器,websockt不支持,同样是自定义
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
// 服务拦截器,主要是发送(write、input)、读取(read、output)数据
interceptors.add(new CallServerInterceptor(forWebSocket));
// 开启调用链
Interceptor.Chain chain = new RealInterceptorChain(
interceptors, , originalRequest);
return chain.proceed(originalRequest);
}
其逻辑大致分为两部分:
创建一系列拦截器,并将其放入一个拦截器数组中。这部分拦截器即包括用户自定义的拦截器也包括框架内部拦截器
创建一个拦截器链RealInterceptorChain,并执行拦截器链的proceed方法
接下来看下RealInterceptorChain的实现逻辑:
public final class RealInterceptorChain implements Interceptor.Chain {
private final List interceptors;
private final StreamAllocation streamAllocation;
private final HttpCodec httpCodec;
private final RealConnection connection;
private final int index;
private final Request request;
private int calls;
public RealInterceptorChain(List interceptors, StreamAllocation streamAllocation,
HttpCodec httpCodec, RealConnection connection, int index, Request request) {
this.interceptors = interceptors;
this.connection = connection;
this.streamAllocation = streamAllocation;
this.httpCodec = httpCodec;
this.index = index;
this.request = request;
}
@Override public Connection connection() {
return connection;
}
public StreamAllocation streamAllocation() {
return streamAllocation;
}
public HttpCodec httpStream() {
return httpCodec;
}
@Override public Request request() {
return request;
}
@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 {
…
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
Android进阶资料
以下的资料是近年来,我和一些朋友面试收集整理了很多大厂的面试真题和资料,还有来自如阿里、小米、爱奇艺等一线大厂的大牛整理的架构进阶资料。希望可以帮助到大家。
Android进阶核心笔记
百万年薪必刷面试题
最全Android进阶学习视频
ndroid进阶核心笔记**
[外链图片转存中…(img-33ZjMh7R-1711939399739)]
百万年薪必刷面试题
[外链图片转存中…(img-bvrnx8Xo-1711939399739)]
最全Android进阶学习视频