基本使用——OkHttp3詳細使用教程

基本使用——OkHttp3詳細使用教程

概述

OkHttp如今應該算是最火的Http第三方庫,Retrofit底層也是使用OkHttp,網上不少教程都寫的不錯,可是有些我認爲重要的知識,大多一筆帶過,因此我決定寫一篇入門文章php

OkHttp官網地址:http://square.github.io/okhttp/
OkHttp GitHub地址:https://github.com/square/okhttphtml

出現背景

網絡訪問的高效性要求,能夠說是爲高效而生java

解決思路

  1. 提供了對 HTTP/2 和 SPDY 的支持,這使得對同一個主機發出的全部請求均可以共享相同的套接字鏈接
  2. 若是 HTTP/2 和 SPDY 不可用,OkHttp 會使用鏈接池來複用鏈接以提升效率
  3. 提供了對 GZIP 的默認支持來下降傳輸內容的大小
  4. 提供了對 HTTP 響應的緩存機制,能夠避免沒必要要的網絡請求
  5. 當網絡出現問題時,OkHttp 會自動重試一個主機的多個 IP 地址

OkHttp3設計思路

這裏寫圖片描述

Requests(請求)android

每個HTTP請求中都應該包含一個URL,一個GET或POST方法以及Header或其餘參數,固然還能夠含特定內容類型的數據流。git

Responses(響應)github

響應則包含一個回覆代碼(200表明成功,404表明未找到),Header和定製可選的body。web

2、使用教程

2.一、GRADLE引入包

compile 'com.squareup.okhttp3:okhttp:3.2.0'
  • 1

2.二、建立OkHttpClient實例

簡單來講,經過OkHttpClient能夠發送一個Http請求,並讀取該Http請求的響應,它是一個生產Call的工廠。
此外,受益於一個共享的響應緩存/線程池/複用的鏈接等因素,絕大多數應用使用一個OkHttpClient實例,即可以知足整個應用的Http請求。json

三種建立實例的方法:api

  • 建立一個默認配置OkHttpClient,可使用默認的構造函數。
  • 經過new OkHttpClient.Builder()方法來一步一步配置一個OkHttpClient實例。
  • 若是要求使用現有的實例,能夠經過newBuilder()方法來進行構造。
OkHttpClient client = new OkHttpClient(); OkHttpClient clientWith30sTimeout = client.Builder() .readTimeout(30, TimeUnit.SECONDS) .build(); OkHttpClient client = client.newBuilder().build();

看一下OkHttpClient的源碼,會發現緩存/代理等等需求,包羅萬象的按照類封裝到了Builder中。緩存

Dispatcher dispatcher;          // 分發 Proxy proxy; // 代理 List<Protocol> protocols; List<ConnectionSpec> connectionSpecs; final List<Interceptor> interceptors = new ArrayList<>(); // 攔截器 final List<Interceptor> networkInterceptors = new ArrayList<>(); // 網絡攔截器 ProxySelector proxySelector; CookieJar cookieJar; Cache cache; // 緩存 InternalCache internalCache; SocketFactory socketFactory; SSLSocketFactory sslSocketFactory; HostnameVerifier hostnameVerifier; CertificatePinner certificatePinner; Authenticator proxyAuthenticator; // 代理證書 Authenticator authenticator; // 證書 ConnectionPool connectionPool; Dns dns; // DNS boolean followSslRedirects; boolean followRedirects; boolean retryOnConnectionFailure; int connectTimeout; int readTimeout; int writeTimeout;

2.三、GET

OkHttpClient client = new OkHttpClient(); String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); }

Request

簡單看一下Request類,能夠發現它表明一個Http請求,須要注意的是Request一旦build()以後,便不可修改。

主要經過new Request.Builder()來一步一步構造的。看一下Builder的代碼。

public Builder() { this.method = "GET"; this.headers = new Headers.Builder(); }

默認是Get方法,
此外還建立了頭信息。Headers類中是經過List<String> namesAndValues = new ArrayList<>(20),來存放頭信息的,一開始我也很納悶,頭信息都是一對一對的爲何要用List,看一下源碼發現,在存取的時候都是將索引+2或者-2。而且頭信息能夠存在多個相同的Key信息。

發起請求

跟到newCall()方法中發現,又使用OkHttpClient實例和Request的實例,一塊兒構造了一個RealCall的實例。

RealCall類簡單作了一個託管並經過Dispather類對請求進行分發和執行,實際開啓線程發起請求的方法就在這個類中。

隨後又調用execute()方法,拿到了一個響應。這個execute()方法,實際上執行的就是RealCall中的execute()方法,最後調用了Dispatcher的execute()方法。

Response

Response表明一個Http的響應,這個類的實例不可修改。

一個簡單的Get請求和說明就結束了

2.四、POST

2.4.一、POST提交字符串

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } 

MediaType用於描述Http請求和響應體的內容類型,也就是Content-Type

一次請求就是向目標服務器發送一串文本。什麼樣的文本?有下面結構的文本。
HTTP請求包結構(圖片來自Android網絡請求心路歷程

這裏寫圖片描述

例子:

POST /meme.php/home/user/login HTTP/1.1 Host: 114.215.86.90 Cache-Control: no-cache Postman-Token: bd243d6b-da03-902f-0a2c-8e9377f6f6ed Content-Type: application/x-www-form-urlencoded tel=13637829200&password=123456

例如,MediaType.parse(「application/json; charset=utf-8」);這個就帶表請求體的類型爲JSON格式的。

定義好數據類型,還要將其變爲請求體,最後經過post()方法,隨請求一併發出。

2.4.二、POST提交鍵值對

OkHttp也能夠經過POST方式把鍵值對數據傳送到服務器

OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody formBody = new FormEncodingBuilder() .add("platform", "android") .add("name", "bug") .add("subject", "XXXXXXXXXXXXXXX") .build(); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } }

2.4.三、Post方式提交流

以流的方式POST提交請求體。請求體的內容由流寫入產生。這個例子是流直接寫入Okio的BufferedSink。你的程序可能會使用OutputStream,你可使用BufferedSink.outputStream()來獲取。.

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()); }

重寫RequestBody中的幾個方法,將本地數據放入到Http協議的請求體中,而後發送到服務端。

2.4.四、Post方式提交表單

使用FormEncodingBuilder來構建和HTML標籤相同效果的請求體。鍵值對將使用一種HTML兼容形式的URL編碼來進行編碼。

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()); }

2.4.五、Post方式提交分塊請求,能夠上傳文件

MultipartBuilder能夠構建複雜的請求體,與HTML文件上傳形式兼容。

多塊請求體中每塊請求都是一個請求體,能夠定義本身的請求頭。這些請求頭能夠用來描述這塊請求,例如他的Content-Disposition。若是Content-Length和Content-Type可用的話,他們會被自動添加到請求頭中。

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()); }

2.五、HTTP頭部的設置和讀取

HTTP 頭的數據結構是 Map<String, List<String>>類型。也就是說,對於每一個 HTTP 頭,可能有多個值。可是大部分 HTTP 頭都只有一個值,只有少部分 HTTP 頭容許多個值。至於name的取值說明,能夠查看這個請求頭大全

OkHttp的處理方式是:

  • 使用header(name,value)來設置HTTP頭的惟一值,若是請求中已經存在響應的信息那麼直接替換掉。
  • 使用addHeader(name,value)來補充新值,若是請求頭中已經存在name的name-value,那麼還會繼續添加,請求頭中便會存在多個name相同而value不一樣的「鍵值對」。
  • 使用header(name)讀取惟一值或多個值的最後一個值
  • 使用headers(name)獲取全部值
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://github.com") .header("User-Agent", "My super agent") .addHeader("Accept", "text/html") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException("服務器端錯誤: " + response); } System.out.println(response.header("Server")); System.out.println(response.headers("Set-Cookie"));

2.六、同步和異步

Synchronous Get(同步Get)

下載一個文件,打印他的響應頭,以string形式打印響應體。

響應體的 string() 方法對於小文檔來講十分方便、高效。可是若是響應體太大(超過1MB),應避免適應 string()方法 ,由於他會將把整個文檔加載到內存中。對於超過1MB的響應body,應使用流的方式來處理body。

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()); }

Asynchronous Get(異步Get)

在一個工做線程中下載文件,當響應可讀時回調Callback接口。讀取響應時會阻塞當前線程。OkHttp現階段不提供異步api來接收響應體。

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官方教程解析-完全入門OkHttp使用
Android OkHttp徹底解析 是時候來了解OkHttp了
OKHttp3.0的平常及入門
#Android#OkHttp3使用指南

相關文章
相關標籤/搜索