預備知識: HTTP協議詳解 HTTP請求報文和響應報文php
OkHttp3是由square公司開發,Android中公認最好用的網絡請求框架,在接口封裝上作的簡單易用,GitHub地址。css
它有如下默認特性:html
當網絡出現問題的時候OkHttp 會自動恢復通常的鏈接問題,若是你的服務有多個IP地址,當第一個IP請求失敗時,OkHttp會交替嘗試你配置的其餘IP。java
gradle引入依賴便可。android
implementation 'com.squareup.okhttp3:okhttp:3.14.7'
implementation 'com.squareup.okio:okio:1.17.5'
複製代碼
3.14.x版本及之前的版本,採用Java語言編寫,4.0.0之後採用kotlin語言;本系列文章中源碼引自3.14.x版本,以Java語言講解。git
其中Okio庫 是對Java.io和java.nio的補充,以便可以更加方便,快速的訪問、存儲和處理你的數據。OkHttp的底層使用該庫做爲支持。github
另外,別忘了申請網絡請求權限,若是還使用網絡請求的緩存功能,那麼還要申請讀寫外存的權限:json
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
複製代碼
基本使用步驟以下api
下面跟着具體使用實例,詳細介紹。緩存
以百度主頁爲例,進行Get請求:
OkHttpClient httpClient = new OkHttpClient();
String url = "https://www.baidu.com/";
Request getRequest = new Request.Builder()
.url(url)
.get()
.build();
Call call = httpClient.newCall(getRequest);
new Thread(new Runnable() {
@Override
public void run() {
try {
//同步請求,要放到子線程執行
Response response = call.execute();
Log.i(TAG, "okHttpGet run: response:"+ response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
複製代碼
首先,建立了OkHttpClient實例,接着用Request.Builder構建了Request實例並傳入了百度主頁的url,而後httpClient.newCall方法傳入Request實例生成call,最後在子線程調用call.execute()執行請求得到結果response。
因此,使用OkHttp進行get請求,是比較簡單的,只要在構建Request實例時更換url就能夠了。
有個問題,你可能注意到了,這裏是放在子線程執行請求的,這是由於call.execute()是同步方法。想要在主線程直接使用而不用手動建立子線程能夠嘛?固然能夠,使用call.enqueue(callback)便可:
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, "okHttpGet enqueue: onResponse:"+ response.body().string());
}
});
複製代碼
call.enqueue會異步執行,須要注意的是,兩個回調方法onFailure、onResponse是執行在子線程的,因此若是想要執行UI操做,須要使用Handler切換到UI線程。
另外,注意每個Call只能執行一次(緣由會在下篇流程分析中說明)。
執行後結果打印以下:
2020-05-04 21:52:56.446 32681-3631/com.hfy.androidlearning I/OkHttpTestActivity: okHttpGet run: response:<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登陸</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登陸</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
複製代碼
可見百度首頁的get請求成功響應了。
post請求與get請求的區別是 在構造Request對象時,須要多構造一個RequestBody對象,用它來攜帶咱們要提交的數據。示例以下:
OkHttpClient httpClient = new OkHttpClient();
MediaType contentType = MediaType.parse("text/x-markdown; charset=utf-8");
String content = "hello!";
RequestBody body = RequestBody.create(contentType, content);
Request getRequest = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(body)
.build();
Call call = httpClient.newCall(getRequest);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, "okHttpPost enqueue: \n onResponse:"+ response.toString() +"\n body:" +response.body().string());
}
});
複製代碼
對比get請求,把構建Request時的get()改爲post(body),並傳入RequestBody實例。RequestBody實例是經過create方法建立,須要指定請求體內容類型、請求體內容。這裏是傳入了一個指定爲markdown格式的文本。
結果打印以下:
2020-05-05 13:18:26.445 13301-13542/com.hfy.androidlearning I/OkHttpTestActivity: okHttpPost enqueue:
onResponse:Response{protocol=http/1.1, code=200, message=OK, url=https://api.github.com/markdown/raw}
body:<p>hello!</p>
複製代碼
請求成功並把請求體內容又返回來了。
傳入RequestBody的 MediaType 還能夠是其餘類型,如客戶端要給後臺發送json字符串、發送一張圖片,那麼能夠定義爲:
// RequestBody:jsonBody,json字符串
String json = "jsonString";
RequestBody jsonBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
//RequestBody:fileBody, 上傳文件
File file = new File(Environment.getExternalStorageDirectory(), "1.png");
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
複製代碼
MediaType更多類型信息能夠查看 RFC 2045。
構建RequestBody除了上面的方式,還有它的子類FormBody,FormBody用於提交表單鍵值對,這種能知足日常開發大部分的需求。
//RequestBody:FormBody,表單鍵值對
RequestBody formBody = new FormBody.Builder()
.add("username", "hfy")
.add("password", "qaz")
.build();
複製代碼
FormBody是經過FormBody.Builder用構建者模式建立,add鍵值對便可。它的contentType在內部已經指定了。
private static final MediaType CONTENT_TYPE = MediaType.get("application/x-www-form-urlencoded");
複製代碼
RequestBody另外一個子類MultipartBody,用於post請求提交複雜類型的請求體。複雜請求體能夠同時包含多種類型的的請求體數據。
上面介紹的 post請求 string、文件、表單,只有單一類型。考慮一種場景--註冊場景,用戶填寫完姓名、電話,同時要上傳頭像圖片,這時註冊接口的請求體就須要 接受 表單鍵值對 以及文件了,那麼前面講的的post就沒法知足了。那麼就要用到MultipartBody了。 完整代碼以下:
OkHttpClient httpClient = new OkHttpClient();
// MediaType contentType = MediaType.parse("text/x-markdown; charset=utf-8");
// String content = "hello!";
// RequestBody body = RequestBody.create(contentType, content);
//RequestBody:fileBody,上傳文件
File file = drawableToFile(this, R.mipmap.bigpic, new File("00.jpg"));
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
//RequestBody:multipartBody, 多類型 (用戶名、密碼、頭像)
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("username", "hufeiyang")
.addFormDataPart("phone", "123456")
.addFormDataPart("touxiang", "00.png", fileBody)
.build();
Request getRequest = new Request.Builder()
.url("http://yun918.cn/study/public/file_upload.php")
.post(multipartBody)
.build();
Call call = httpClient.newCall(getRequest);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "okHttpPost enqueue: \n onFailure:"+ call.request().toString() +"\n body:" +call.request().body().contentType()
+"\n IOException:"+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, "okHttpPost enqueue: \n onResponse:"+ response.toString() +"\n body:" +response.body().string());
}
});
複製代碼
可見,在構建RequestBody時是使用MultipartBody.Builder構建了MultipartBody實例,經過addFormDataPart方法傳入了姓名、電話的鍵值對,也經過addFormDataPart("touxiang", "00.png", fileBody)傳入了頭像圖片,其中"touxiang"是key值, "00.png"是文件名,fileBody是要以上傳的圖片建立的RequestBody。 由於全部數據都是以鍵值對的表單形式提交,因此要設置setType(MultipartBody.FORM)。
請求抓包結果:
其餘請求方式像put、header、delete,主要在構建Request時把get()或post()換成put()、header()、delete()就能夠了,但通常在Android端不多用到。
先看幾個問題:
這些問題,在OkHttp這裏很簡單。把OkHttpClient實例的建立,換成如下方式便可:
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.cache(new Cache(getExternalCacheDir(),500 * 1024 * 1024))
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String url = request.url().toString();
Log.i(TAG, "intercept: proceed start: url"+ url+ ", at "+System.currentTimeMillis());
Response response = chain.proceed(request);
ResponseBody body = response.body();
Log.i(TAG, "intercept: proceed end: url"+ url+ ", at "+System.currentTimeMillis());
return response;
}
})
.build();
複製代碼
這裏經過OkHttpClient.Builder經過構建者模式設置了鏈接、讀取、寫入的超時時長,用cache()方法傳入了由緩存目錄、緩存大小構成的Cache實例,這樣就解決了前兩個問題。
還注意到,使用addInterceptor()方法添加了Interceptor實例,且重寫了intercept方法。Interceptor意爲攔截器,intercept()方法會在開始執行請求時調用。其中chain.proceed(request)內部是真正請求的過程,是阻塞操做,執行完後會就會獲得請求結果ResponseBody,因此chain.proceed(request)的先後取當前時間,那麼就知道整個請求所耗費的時間。上面chain.proceed(request)的先後分別打印的日誌和時間,這樣第三個問題也解決了。
具體Interceptor是如何工做,會在下一篇流程分析中介紹。
另外,一般OkHttpClient實例是全局惟一的,這樣這些基本配置就是統一,且內部維護的鏈接池也能夠有效複用(會在下一篇流程分析中介紹)。
全局配置的有了,單個請求的也能夠有一些單獨的配置。
Request getRequest = new Request.Builder()
.url("http://yun918.cn/study/public/file_upload.php")
.post(multipartBody)
.addHeader("key","value")
.cacheControl(CacheControl.FORCE_NETWORK)
.build();
複製代碼
這個Request實例,