写点什么

OkHttp3 源码详解 (三) 拦截器,android 面试自我介绍范文

作者:嘟嘟侠客
  • 2021 年 11 月 28 日
  • 本文字数:2090 字

    阅读完需:约 7 分钟

  1. @Override

  2. public Call newCall(Request request) {

  3. return new RealCall(this, request, false /* for web socket */);

  4. }


RealCall.enqueue实际就是讲一个RealCall放入到任务队列中,等待合适的机会执行:


  1. @Override

  2. public void enqueue(Callback responseCallback) {

  3. synchronized (this) {

  4. if (executed) throw new IllegalStateException("Already Executed");

  5. executed = true;

  6. }

  7. captureCallStackTrace();

  8. client.dispatcher().enqueue(new AsyncCall(responseCallback));

  9. }


从代码中可以看到最终RealCall被转化成一个AsyncCall并被放入到任务队列中,任务队列中的分发逻辑这里先不说,相关实现会放在OkHttp源码分析——任务队列疑问进行介绍。这里只需要知道 AsyncCall 的 excute 方法最终将会被执行:


  1. [RealCall.java]

  2. @Override protected void execute() {

  3. boolean signalledCallback = false;

  4. try {

  5. Response response = getResponseWithInterceptorChain();

  6. if (retryAndFollowUpInterceptor.isCanceled()) {

  7. signalledCallback = true;

  8. responseCallback.onFailure(RealCall.this, new IOException("Canceled"));

  9. } else {

  10. signalledCallback = true;

  11. responseCallback.onResponse(RealCall.this, response);

  12. }

  13. } catch (IOException e) {

  14. if (signalledCallback) {

  15. // Do not signal the callback twice!


《Android 学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享


Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);


  1. } else {

  2. responseCallback.onFailure(RealCall.this, e);

  3. }

  4. } finally {

  5. client.dispatcher().finished(this);

  6. }

  7. }

  8. }


execute 方法的逻辑并不复杂,简单的说就是:


调用 getResponseWithInterceptorChain 获取服务器返回


通知任务分发器(client.dispatcher)该任务已结束


getResponseWithInterceptorChain 构建了一个拦截器链,通过依次执行该拦截器链中的每一个拦截器最终得到服务器返回。


3. 构建拦截器链


首先来看下 getResponseWithInterceptorChain 的实现:


源码路径:okhttp3/RealCall.java


// 开始执行整个请求


Response getResponseWithInterceptorChain() throws IOException {


// Build a full stack of interceptors.


// 拦截器栈


List<Interceptor> 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<Interceptor> 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<Interceptor> 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;


}

尾声

在我的博客上很多朋友都在给我留言,需要一些系统的面试高频题目。之前说过我的复习范围无非是个人技术博客还有整理的笔记,考虑到笔记是手写版不利于保存,所以打算重新整理并放到网上,时间原因这里先列出面试问题,题解详见:



展示学习笔记




本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

用户头像

嘟嘟侠客

关注

还未添加个人签名 2021.03.19 加入

还未添加个人简介

评论

发布
暂无评论
OkHttp3源码详解(三) 拦截器,android面试自我介绍范文