日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Okhttp源码解析(三)——责任链

發布時間:2024/10/12 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Okhttp源码解析(三)——责任链 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考:https://www.jianshu.com/p/e3b6f821acb8

上文提到

請求的具體執行在RealCall.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);}

  

一.攔截器模式的實現

  責任鏈構造器

public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,HttpStream httpStream, Connection connection, int index, Request request) {this.interceptors = interceptors;this.connection = connection;this.streamAllocation = streamAllocation;this.httpStream = httpStream;this.index = index;this.request = request;}

  

public Response proceed(Request request, StreamAllocation streamAllocation, HttpStream httpStream,Connection connection) throws IOException {
    //省略一部分
      // 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);
}

  我們看到proceed()就是執行攔截器集合的第index個攔截器,同時intercept方法的參數又是攔截器鏈,但是該攔截器鏈不用this而重新構造一個新的在于構造器參數index設為了index+1

  我們假設用戶沒有自定義攔截器,則第一個攔截器為RetryAndFollowUpInterceptor。

  

@Override public Response intercept(Chain chain) throws IOException {
  //省略一部分
   Response response = null;
boolean releaseConnection = true;
try {
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
  //省略
}

  我們看到該攔截器的intercepte方法又是執行攔截器鏈的proceed方法,相當于就是執行第index個攔截器,相對本攔截器就是下一個攔截器,同時在proceed方法構造責任器鏈時index又+1了,為了下下個攔截器做準備。以此類推達到責任鏈模式。

  圖示:

  

?

?

?

  PS

  我們看著這攔截器模式有點像Android事件分發機制,例如點擊時間在控件上的傳遞,但是有點不一樣的是事件分發機制在事件分發的過程中,無論怎么分發,處理者只能為一個,但是攔截器在分發后處理返回回來,自生可以在結果的基礎上在做一層處理。

    

轉載于:https://www.cnblogs.com/could-deng/p/8378726.html

總結

以上是生活随笔為你收集整理的Okhttp源码解析(三)——责任链的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。