OkHttp 源码解析---拦截器

拦截器的执行顺序是是什么?


okhttp3.RealCall#getResponseWithInterceptorChain

  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    interceptors.add(new RetryAndFollowUpInterceptor(client));
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, transmitter, null, 0,
        originalRequest, this, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    boolean calledNoMoreExchanges = false;
    try {
      Response response = chain.proceed(originalRequest);
      if (transmitter.isCanceled()) {
        closeQuietly(response);
        throw new IOException("Canceled");
      }
      return response;
    } catch (IOException e) {
      calledNoMoreExchanges = true;
      throw transmitter.noMoreExchanges(e);
    } finally {
      if (!calledNoMoreExchanges) {
        transmitter.noMoreExchanges(null);
      }
    }
  }

这里有定义顺序,我们也可以从调用堆栈里面看到整个拦截器执行的顺序。

调用堆栈

"OkHttp https://www.baidu.com/...@9689" prio=5 tid=0xae nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:37)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	  at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	  at com.example.okhttptest.MainActivity$1.intercept(MainActivity.java:27)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
	  at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)
	  at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
	  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
	  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
	  at java.lang.Thread.run(Thread.java:919)

如何拦截替换请求

        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder().url("https://www.sina.com.cn/").build();

                return chain.proceed(request);
            }
        }).build();

如果手机没有网,那么重试拦截器会重试吗?

不会

各层拦截器的作用是什么?

在 RealCall 中会按照顺序添加如下几个默认的 Interceptor 到责任链中用来完成基本功能:

用户设置的 Interceptor
RetryAndFollowUpInterceptor:失败重试及重定向
BridgeInterceptor:处理网络 Header、Cookie、gzip 等
CacheInterceptor:管理缓存
ConnectInterceptor:连接服务器
如果是 WebSocket 请求则添加对应的 Interceptors
CallServerInterceptor:数据发送/接收

networkInterceptors 和 Interceptors 的区别是什么?

执行顺序不一样,如果遇到304 重定向,netWorkInteceptors 能够监听到,而普通的application interceptors监听不到。application interceptors 只调用一次,不会处理到304.

所有的拦截器没有使用for 循环,是怎么进行按照顺序调用的?

RealInterceptorChain 调用 第一个 Interceptors 的 process 方法,第一个Interceptors 的process 方法中会调用下一个拦截器的intercept 方法,

okhttp3.internal.http.RealInterceptorChain#proceed() 注定会创建一个新的RealInterceptorChain,然后调用下一个拦截器的intercept 方法,并传入下一个chain,interceptor 处理完就调用chain 的process,chain 的 process 会获取到下一个拦截器,并传递下一次chain,就可以层层传递。

异步任务线程的名字在哪里定义的?

OkHttp https://www.baidu.com/…



public abstract class NamedRunnable implements Runnable {
  protected final String name;

  public NamedRunnable(String format, Object... args) {
    this.name = Util.format(format, args);
  }

  @Override public final void run() {
    String oldName = Thread.currentThread().getName();
    Thread.currentThread().setName(name);
    try {
      execute();
    } finally {
      Thread.currentThread().setName(oldName);
    }
  }

  protected abstract void execute();
}

一些参考:

https://zhuanlan.zhihu.com/p/104813091

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值