OkHttp如今應該算是最火的Http第三方庫,Retrofit底層也是使用OkHttp,網上不少教程都寫的不錯,可是有些我認爲重要的知識,大多一筆帶過,因此我決定寫一篇入門文章php
OkHttp官網地址:http://square.github.io/okhttp/
OkHttp GitHub地址:https://github.com/square/okhttphtml
網絡訪問的高效性要求,能夠說是爲高效而生java
Requests(請求)android
每個HTTP請求中都應該包含一個URL,一個GET或POST方法以及Header或其餘參數,固然還能夠含特定內容類型的數據流。git
Responses(響應)github
響應則包含一個回覆代碼(200表明成功,404表明未找到),Header和定製可選的body。web
compile 'com.squareup.okhttp3:okhttp:3.2.0'
簡單來講,經過OkHttpClient能夠發送一個Http請求,並讀取該Http請求的響應,它是一個生產Call的工廠。
此外,受益於一個共享的響應緩存/線程池/複用的鏈接等因素,絕大多數應用使用一個OkHttpClient實例,即可以知足整個應用的Http請求。json
三種建立實例的方法:api
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;
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類,能夠發現它表明一個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表明一個Http的響應,這個類的實例不可修改。
一個簡單的Get請求和說明就結束了
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()方法,隨請求一併發出。
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); } }
以流的方式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協議的請求體中,而後發送到服務端。
使用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()); }
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()); }
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"));
下載一個文件,打印他的響應頭,以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()); }
在一個工做線程中下載文件,當響應可讀時回調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使用指南