okHttp源碼解析------待續

來源:互聯網
上載者:User

標籤:步驟   eal   sock   使用   web   並發   cep   tar   class   

 

看該篇文章前首先要熟悉okHttp的使用,建議先讀OkHttp的簡單使用

本文的源碼解析參考連結:okhttp3總和解析

1.從URL請求處理開始分析

由非同步將請求加入調度方法開始引入正題:

getClient().newCall(request).enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                    }});
/**   * Prepares the {@code request} to be executed at some point in the future.   */  @Override public Call newCall(Request request) {    return new RealCall(this, request);  }
@Override public void enqueue(Callback responseCallback) {    enqueue(responseCallback, false);  }  void enqueue(Callback responseCallback, boolean forWebSocket) {    synchronized (this) {      if (executed) throw new IllegalStateException("Already Executed");      executed = true;    }    client.dispatcher().enqueue(new AsyncCall(responseCallback, forWebSocket));  }

由源碼上的調用順序,我們將步驟轉化為以下的幾個技術點:

 

由以上的使用okhttp請求的調用代碼,可以看到最終的請求時通過dispatcher來完成的。

2.Dispatcher調度者

Dispatcher進行線程分發,有兩個方法,一個是普通的單線程,也就是線程阻塞的方式請求;另一種就是通過隊列進行並發任務的分發和回調,下面主要分析第二種:

 

3.Interceptor攔截器

 

4.緩衝策略

 

4.Socket管理(StreamAllocation)

 

5.HTTP請求序列化/還原序列化

 

6電腦常識

 

okHttp源碼解析------待續

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.