OkHttp3-使用進階(Recipes)

轉載請注明出處 http://www.lxweimin.com/p/25e89116847c (作者:韓棟)
本文為譯文,由于譯者水平有限,歡迎拍磚,讀者也可以閱讀原文
OkHttp3-基本用法,OkHttp3-使用進階(Recipes),OkHttp3-請求器(Calls)OkHttp3-連接(Connections)OkHttp3-攔截器(Interceptor)


我們寫了一些例子用來演示如何解決在OkHttp遇到的常見問題。通過這些例子去學習關于OkHttp中的組件是如何一起工作的。可以隨意復制粘貼所需要的代碼。

Synchronous Get(Get方式同步請求)

private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
      System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }

    System.out.println(response.body().string());
  }

這個例子我們演示了一個下載文件并且以字符串的形式打印出它的響應頭和響應主體的例子。

這個response.body().string()中的string()方法在對于小文檔來說是非常方便和高效的。但是如果這個響應主體比較大(超過1MiB),那么應該避免使用string()方法,因為它將會把整個文檔一次性加載進內存中(容易造成oom)。在這種情況下,建議將這個響應主體作為數據流來處理。

Asynchronous Get(Get方式異步請求)

 private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();

    client.newCall(request).enqueue(new Callback() {
      @Override public void onFailure(Call call, IOException e) {
        e.printStackTrace();
      }

      @Override public void onResponse(Call call, Response response) throws IOException {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        Headers responseHeaders = response.headers();
        for (int i = 0, size = responseHeaders.size(); i < size; i++) {
          System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }

        System.out.println(response.body().string());
      }
    });
  }

在這個例子中,我們在子線程中下載一個文件,并且在對應的回調方法在主線程中讀取響應。注意:讀取大量的響應主體可能會堵塞主線程。OkHttp當前不提供異步Api用來分段獲取響應主體。

Accessing Headers(訪問頭部信息)

 private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("https://api.github.com/repos/square/okhttp/issues")
        .header("User-Agent", "OkHttp Headers.java")
        .addHeader("Accept", "application/json; q=0.5")
        .addHeader("Accept", "application/vnd.github.v3+json")
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println("Server: " + response.header("Server"));
    System.out.println("Date: " + response.header("Date"));
    System.out.println("Vary: " + response.headers("Vary"));
  }

通常Http的以一個Map<String, String>數據結構來存儲Http頭部信息:每一個Key所對應的Value是可空的,但是有一些字段是允許存在多個的 (可以使用Multimap,簡單說下,MultimapMap最大的不同就是前者的Key可以重復)。比如,一個Http響應提供了多個合法并且常用的Key為Vary響應頭信息。那么OkHttp的Api將會生成多個合適的方案。(Vary 字段用于列出一個響應字段列表,告訴緩存服務器遇到同一個 URL 對應著不同版本文檔的情況時,如何緩存和篩選合適的版本。)

為請求添加請求頭字段有兩種方式,header(name, value)addHeader(name, value)。唯一不同的是前者會覆蓋掉原有的字段(如果原來存在此字段),后者則是在原來的字段信息進行添加,不會覆蓋。

讀取響應頭信息也有兩種方式,header(name)headers(name)。前者只會返回對應的字段最后一次出現的值,后者則是將對應的字段所有的值返回。當然,值是允許為null的。

如果你想得到所有的頭部信息,使用Headers類是個很好的主意。它支持通過索引來獲取頭部信息。

Post a String(上傳一個字符串)

public static final MediaType MEDIA_TYPE_MARKDOWN
      = MediaType.parse("text/x-markdown; charset=utf-8");

  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    String postBody = ""
        + "Releases\n"
        + "--------\n"
        + "\n"
        + " * _1.0_ May 6, 2013\n"
        + " * _1.1_ June 15, 2013\n"
        + " * _1.2_ August 11, 2013\n";

    Request request = new Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

上面的例子使用一個Post的請求方式,將一個字符串放在HTML的格式的文本中上傳到服務器。這種方式的上傳數據方式是將整個請求體一次性放入內存中,所以當所需上傳的數據大小超過1Mib時,應當避免使用這種方式上傳數據,因為會對程序的性能損害,甚至oom。當上傳的數據大小超過此值時,可以以數據流的形式上傳。

Post Streaming(以數據流的形式上傳)

public static final MediaType MEDIA_TYPE_MARKDOWN
      = MediaType.parse("text/x-markdown; charset=utf-8");

  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    RequestBody requestBody = new RequestBody() {
      @Override public MediaType contentType() {
        return MEDIA_TYPE_MARKDOWN;
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        sink.writeUtf8("Numbers\n");
        sink.writeUtf8("-------\n");
        for (int i = 2; i <= 997; i++) {
          sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
        }
      }

      private String factor(int n) {
        for (int i = 2; i < n; i++) {
          int x = n / i;
          if (x * i == n) return factor(x) + " × " + i;
        }
        return Integer.toString(n);
      }
    };

    Request request = new Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(requestBody)
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

這個例子展示了通過數據流的方式上傳一個和上個例子同樣的字符串。如果你是以依賴的方式使用OkHttp這個庫,那么就無需再手動為它添加Okio庫了。因為OkHttp默認依賴于OkioOkio為OkHttp提供了一個可控大小的緩存池,這樣我們就不用擔心因為上傳大數據而會出現的問題。如果你更傾向使用OutputStream,你可以通過BufferedSink.outputStream()獲取到OutputStream對象。

Posting a File(上傳一個文件)

  public static final MediaType MEDIA_TYPE_MARKDOWN
      = MediaType.parse("text/x-markdown; charset=utf-8");

  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    File file = new File("README.md");

    Request request = new Request.Builder()
        .url("https://api.github.com/markdown/raw")
        .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

這個例子很簡單展示了如何將一個文件上傳至服務器。

Posting form parameters(上傳表單參數)

private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    RequestBody formBody = new FormBody.Builder()
        .add("search", "Jurassic Park")
        .build();
    Request request = new Request.Builder()
        .url("https://en.wikipedia.org/w/index.php")
        .post(formBody)
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

使用 FormBody.Builder去構建一個像HTML<form>標簽一樣的請求體。

Posting a multipart request(上傳攜帶有多種表單數據的主體)

  private static final String IMGUR_CLIENT_ID = "...";
  private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("title", "Square Logo")
        .addFormDataPart("image", "logo-square.png",
            RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
        .build();

    Request request = new Request.Builder()
        .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
        .url("https://api.imgur.com/3/image")
        .post(requestBody)
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

通過MultipartBody.Builder可以創建擁有復雜的請求主體的請求,比如有多種需要上傳的數據格式的表單數據。它們每一種表單數據都是一個請求主體,你可以為它們分別定義屬于每個請求主體的請求頭信息,比如Content-Disposition,并且OkHttp會自動為它們添加Content-LengthContent-Type請求頭。

Parse a JSON Reponse With Gson(用Gson來解析Json數據)

 private final OkHttpClient client = new OkHttpClient();
  private final Gson gson = new Gson();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("https://api.github.com/gists/c2a7c39532239ff261be")
        .build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
    for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
      System.out.println(entry.getKey());
      System.out.println(entry.getValue().content);
    }
  }

  static class Gist {
    Map<String, GistFile> files;
  }

  static class GistFile {
    String content;
  }

Gson是一種可將Json數據序列化成Java對象,或者將Java對象反序列化為Json數據。這個例子中我們將從GitHub Api返回響應的Json數據序列化為Gist.class對象。
  需要注意的是,我們這里使用的ResponseBody.charStream()使用的是響應頭中Content-Type的字段為編碼格式,如果此響應頭中沒有對應的字段,那么默認為UTF-8編碼。

Response Caching(響應緩存)

 private final OkHttpClient client;

  public CacheResponse(File cacheDirectory) throws Exception {
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(cacheDirectory, cacheSize);

    client = new OkHttpClient.Builder()
        .cache(cache)
        .build();
  }

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();

    Response response1 = client.newCall(request).execute();
    if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);

    String response1Body = response1.body().string();
    System.out.println("Response 1 response:          " + response1);
    System.out.println("Response 1 cache response:    " + response1.cacheResponse());
    System.out.println("Response 1 network response:  " + response1.networkResponse());

    Response response2 = client.newCall(request).execute();
    if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);

    String response2Body = response2.body().string();
    System.out.println("Response 2 response:          " + response2);
    System.out.println("Response 2 cache response:    " + response2.cacheResponse());
    System.out.println("Response 2 network response:  " + response2.networkResponse());

    System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
  }

為了緩存響應,你必須先有一個可以讀寫以及大小確定的緩存目錄。這個緩存目錄應該是私有的,并且通常情況下其他程序是無法對這個目錄進行讀取的。

多個緩存無法同時訪問一個相同的緩存目錄。否則可能會造成響應數據發生錯誤,甚至程序Crash。為了避免這種情況,我們一般在程序中只調用new OkHttpClient()創建OkHttpClient一次,并且對它進行緩存配置,并且在全局中使用這個實例(我們可以通過單例模式來創建它,不過很多人開發Android的人喜歡在Application中創建它)。

OkHttp會根據響應頭的配置配置信息對響應數據進行緩存。服務器會在返回的響應數據中配置這個響應數據在你的程序中應該被緩存多久,比如Cache-Control: max-age=9600,它緩存配置時間為9600秒,9600秒后它將會過期。但是我們是否可以自定義緩存時間呢,答案是可以的。我們可以在請求頭中添加緩存配置,比如Cache-Control: max-stale=3600,當服務器返回響應時,OkHttp會使用此配置進行緩存。

There are cache headers to force a cached response, force a network response, or force the network response to be validated with a conditional GET.這句話不知道怎么翻譯。。求讀者指教。。囧。。
  
  通常在存在請求的響應緩存并且緩存數據沒有過期的情況下,那么當你再次發送這個請求時,OkHttp并不會去服務器上獲取數據,而是直接在本地緩存目錄中取得數據返回給你。當然,如果你想避免從緩存中獲取數據,那么你可以在構建Request的時候使用cacheControl()方法以CacheControl.FORCE_NETWORK為參數進行配置。或者當緩存過期,但你還是不想去服務器請求,而是再次使用緩存。你也可以配置為CacheControl.FORCE_CACHE。注意:如果此時本地緩存中并沒有緩存數據,或者因為其他原因(不包括緩存過期)而必須去服務器請求,那么OkHttp將會返回一個504 Unsatisfiable Request的響應。

Canceling a Call(取消一個請求器)

 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
        .build();

    final long startNanos = System.nanoTime();
    final Call call = client.newCall(request);

    // Schedule a job to cancel the call in 1 second.
    executor.schedule(new Runnable() {
      @Override public void run() {
        System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
        call.cancel();
        System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
      }
    }, 1, TimeUnit.SECONDS);

    try {
      System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
      Response response = call.execute();
      System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
          (System.nanoTime() - startNanos) / 1e9f, response);
    } catch (IOException e) {
      System.out.printf("%.2f Call failed as expected: %s%n",
          (System.nanoTime() - startNanos) / 1e9f, e);
    }
  }

使用Call.cancel()方法將會立即停止此Call正在運行中的網絡工作(為什么是工作呢,因為此時Call可能在發送請求,或者讀取響應等),此時它可能會拋出一個IOException異常。在適當的時候取消不再需要的請求有利于我們減少程序的工作以及流量的損耗。比如在Android開發中,用戶點擊了返回鍵離開了這個頁面(假如這個Activity或者Fragment被銷毀),那么我們就可以在相應的回調(一般在onDestroy())中取消掉在這個頁面所有的Call的網絡工作,包括異步和同步的都可以取消。

Timeouts(超時)

private final OkHttpClient client;

  public ConfigureTimeouts() throws Exception {
    client = new OkHttpClient.Builder()
        .connectTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(10, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .build();
  }

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
        .build();

    Response response = client.newCall(request).execute();
    System.out.println("Response completed: " + response);
  }

OkHttp支持配置三種超時情況,分別是連接超時、讀取超時以及寫入超時。當發生超時情況時,OkHttp將會調用Call.cancel()來取消掉此Call的所有網絡工作。

Per-call Configuration(為個別的Call添加特別的配置)

 private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://httpbin.org/delay/1") // This URL is served with a 1 second delay.
        .build();

    try {
      // Copy to customize OkHttp for this request.
      OkHttpClient copy = client.newBuilder()
          .readTimeout(500, TimeUnit.MILLISECONDS)
          .build();

      Response response = copy.newCall(request).execute();
      System.out.println("Response 1 succeeded: " + response);
    } catch (IOException e) {
      System.out.println("Response 1 failed: " + e);
    }

    try {
      // Copy to customize OkHttp for this request.
      OkHttpClient copy = client.newBuilder()
          .readTimeout(3000, TimeUnit.MILLISECONDS)
          .build();

      Response response = copy.newCall(request).execute();
      System.out.println("Response 2 succeeded: " + response);
    } catch (IOException e) {
      System.out.println("Response 2 failed: " + e);
    }
  }

在前面我們說過最好以單例模式來創建一個OkHttpClient實例,你可以在這個單例中進行比如代理設置、超時以及緩存等配置,以后當我們需要的時候直接獲取這個單例來使用,達到一種全局配置的效果。現在存在一種需求情況,有一個特殊的Call請求的配置需要發生一些改變,我們首先可以通過OkHttpClient.newBuilder()的方法來復制一個和原來全局單例相同的OkHttpClient對象,注意!是復制,也就是說這個新的復制對象會和原來的單例共享同一個連接池,調度器,以及相同的配置信息。然后再進行對新的復制對象進行自定義的配置,最后讓這個特殊的Call使用。

在這個例子中,我們client.newBuilder()復制了一個新的OkHttpClient對象,并且將它的讀取超時時間重新設置了為500秒。

Handing authentication(配置認證信息)

 private final OkHttpClient client;

  public Authenticate() {
    client = new OkHttpClient.Builder()
        .authenticator(new Authenticator() {
          @Override public Request authenticate(Route route, Response response) throws IOException {
            System.out.println("Authenticating for response: " + response);
            System.out.println("Challenges: " + response.challenges());
            String credential = Credentials.basic("jesse", "password1");
            return response.request().newBuilder()
                .header("Authorization", credential)
                .build();
          }
        })
        .build();
  }

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/secrets/hellosecret.txt")
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }

當我們向一個需要進行身份認證的服務器發送請求時(假設此時我們尚未配置身份認證信息),服務器就會返回一個401 Not Authorized的響應。它意味著我們需要進行身份認證才可以獲取到想要的數據。那么我們如何進行配置呢。其實當OkHttp獲取到401 Not Authorized的響應時,OkHttp會向Authenticator對象獲取證書。Authenticator是一個接口,在這個例子中,我們通過向authenticator()方法添加了一個Authenticator對象為參數。在匿名內部類的實現中我們可以看到,authenticate()方法返回了一個設置了header("Authorization", credential)的請求頭的新的Request對象,這個請求頭中的credential就是身份驗證信息。OkHttp使用這個Request自動幫我們再次發送請求。如果我們沒有添加身份認證信息配置,那么OkHttp會自動中斷此次請求,不會再次幫我們重新發送請求。

當服務器返回401 Not Authorized的響應時,我們可以通過Response.challenges()方法獲取所需要的認證信息要求信息。如果只是簡單需要賬號和密碼時候,我們可以使用Credentials.basic(username, password)對請求頭進行編碼。

為了避免當你提供的身份驗證信息錯誤使服務器一直返回401 Not Authorized的響應而導致程序陷入死循環(無限地重試),你可以在Authenticator接口的實現方法authenticate()中返回null來告訴OkHttp放棄重新請求。

if (credential.equals(response.request().header("Authorization"))) {
    return null; // If we already failed with these credentials, don't retry.
   }

你也可以自定義重試的次數,在超過次數之后放棄重新請求。

if (responseCount(response) >= 3) {
    return null; // If we've failed 3 times, give up.
  }

private int responseCount(Response response) {
    int result = 1;
    while ((response = response.priorResponse()) != null) {
      result++;
    }
    return result;
  }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容