1. 程式人生 > >okHttp各個module介紹

okHttp各個module介紹

上一篇我們簡單介紹了okHttp,瞭解了OkHttp的基本用法,並下載了okhttp的原始碼。本篇我們將一起來看看okhttp原始碼的目錄結構,來大概瞭解下okHttp原始碼下的各個module的作用。

okHttp的構建工具使用的是maven,關於maven可自行google,本文不對此做過多闡述。我們來重點看一下okHttp的專案目錄。在這裡以及之後的原始碼都是okHttp的最新版本 3.8.0。我們可以選擇git工具checkout出 3.8.0的tag,本人使用的是idea自帶的git管理工具,在右下角git分支出點選 選測checkout tag, 輸入3.8.0的分支名字:parent-3.8.0 確認即可, 然後我們來看一下專案的目錄結構截圖:

我們來看看各個module的功能作用,主要的借閱資料是module下的README.MD檔案,讀者也可自行查閱。

benchmarks

README.MD中的說明:

This module allows you to test the performance of HTTP clients.
Running
  1. If you made modifications to `Benchmark` run `mvn compile`.
  2. Run `mvn exec:exec` to launch a new JVM, which will execute the benchmark.

用google caliper來測試HTTP客戶端效能的,具體的使用我們先不加關注。

mockwebserver

README.MD中的說明,我們這裡只截取了介紹部分,具體的使用說明不做說明。

A scriptable web server for testing HTTP clients    
**Motivation**  
This library makes it easy to test that your app Does The Right Thing when it
makes HTTP and HTTPS calls. It lets you specify which responses to return and
then verify that requests were made as expected.  
Because it exercises your full HTTP stack, you can be confident that you're
testing everything. You can even copy & paste HTTP responses from your real web
server to create representative test cases. Or test that your code survives in
awkward-to-reproduce situations like 500 errors or slow-loading responses.  
**Example**   
Use MockWebServer the same way that you use mocking frameworks like
[Mockito](https://github.com/mockito/mockito):
1. Script the mocks.
2. Run application code.
3. Verify that the expected requests were made.

此處省略了具體的demo部分,大致是說MockWebServer是用來在單元測試中來模擬網路請求用的,我們可以模擬一個網路返回來驗證我們的網路呼叫。

okcurl

來看README檔案:

_A curl for the next-generation web._  
OkCurl is an OkHttp-backed curl clone which allows you to test OkHttp's HTTP engine (including
HTTP/2) against web servers.

用來離線測試okhttp引擎的。

okhttp

我們將要研究的專案主體。

okhttp-android-support okhttp-urlconnection okhttp-apache

對其它http引擎的適配。

okhttp-hpacktests

These tests use the [hpack-test-case][1] project to validate OkHttp's HPACK
implementation.  The HPACK test cases are in a separate git submodule, so to
initialize them, you must run:

    git submodule init
    git submodule update





TODO ---- * Add maven goal to avoid manual call to git submodule init. * Make hpack-test-case update itself from git, and run new tests. * Add maven goal to generate stories and a pull request to hpack-test-case to have others validate our output.

這個是用來測試hpack的。HPACK是http 2.0的頭部壓縮演算法。

okhttp-loggin-interceptor

An [OkHttp interceptor][1] which logs HTTP request and response data.

日誌輸出的攔截器,關於interceptor之後再分析OkHttp的程式碼中我們會看到它的具體含義和使用。

okhttp-testing-support

對okhttp測試的一些基礎能力支援。

okhttp-tests

okhttp自己的測試

samples

demo

website

okhttp的介紹說明的靜態網頁。

我們已大致瞭解了okhttp下的各個module,在後面的文章中我們將開始逐步進入okhttp的具體程式碼,來一步一步學習okhttp。
這裡寫圖片描述