?一、摘要
現(xiàn)在開(kāi)源的市場(chǎng)上,能網(wǎng)絡(luò)請(qǐng)求的工具非常的多,比如 HttpURLConnection 、Apache HttpClient、okHttp、Retrofit 等等。
雖然可選擇的工具很多,但是作為一名開(kāi)發(fā)者,我們希望在寫(xiě)代碼的時(shí)候,能夠輕松地調(diào)試我們應(yīng)用程序的網(wǎng)絡(luò)通信,選擇適合的工具至關(guān)重要!
就目前我們所熟悉的,其中 Apache HttpClient 因其高效的性能、豐富的 api,在開(kāi)源項(xiàng)目中使用非常廣泛,Android 系統(tǒng)最早默認(rèn)的網(wǎng)絡(luò)請(qǐng)求工具也是使用 Apache HttpClient,但因?yàn)榧嫒菪詥?wèn)題,Android 后期的版本中谷歌不愿意維護(hù)相關(guān)包,改而使用 okHttp。
現(xiàn)在 Android 系統(tǒng)中的網(wǎng)絡(luò)請(qǐng)求框架,基本都是 okhttp 和 Retrofit 一統(tǒng)天下,兩者其實(shí)都是 square 公司出品的,不同的地方在于 Retrofit 是基于 OkHttp 封裝的一套 RESTful 網(wǎng)絡(luò)請(qǐng)求框架,使用方面更加靈活,屬于后起之秀!
既然大家都覺(jué)得 OkHttp 好用,今天我們就一起來(lái)認(rèn)識(shí)一下它!
來(lái)著網(wǎng)上對(duì)于 OkHttp 相關(guān)的介紹如下!
OkHttp 是 Square 公司基于 Java 和 Android 程序,封裝的一個(gè)高性能 http 網(wǎng)絡(luò)請(qǐng)求客戶(hù)端,并且對(duì)外開(kāi)源,它的設(shè)計(jì)初衷是為了更快地加載資源并節(jié)省帶寬。
以下是使用 OkHttp 的主要優(yōu)勢(shì):
- 支持HTTP/2(有效使用套接字)
- 連接池(在沒(méi)有HTTP/2的情況下減少請(qǐng)求延遲)
- GZIP壓縮(縮小下載大小)
- 響應(yīng)緩存(避免了重新獲取相同的數(shù)據(jù))
- 從常見(jiàn)的連接問(wèn)題中無(wú)聲恢復(fù)
- 替代 IP 地址檢測(cè)(在 IPv4 和 IPv6 環(huán)境下)
- 支持現(xiàn)代TLS功能(TLS 1.3,ALPN,證書(shū)釘子)
- 支持同步和異步調(diào)用
目前 OkHttp 在開(kāi)源項(xiàng)目中被廣泛使用,同時(shí)也是 Retrofit、Picasso 等庫(kù)的核心庫(kù)。
既然這么厲害,在網(wǎng)絡(luò)通信中我們?nèi)绾问褂媚?,我們一起?lái)學(xué)習(xí)一下!
二、案例實(shí)踐
2.1、添加依賴(lài)包
在使用之前,我們需要先導(dǎo)入okhttp?依賴(lài)包,不同的版本號(hào),相關(guān) api 稍有區(qū)別,本次介紹的 api 操作基于3.14.9版本號(hào)。
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
2.2、get 同步請(qǐng)求
okhttp?發(fā)起get同步請(qǐng)求非常的簡(jiǎn)單,只需要幾行代碼就可以搞定。
案例如下!
String url = "https://www.baidu.com/";
OkHttpClient client = new OkHttpClient();
// 配置GET請(qǐng)求
Request request = new Request.Builder()
.url(url)
.get()
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 打印返回結(jié)果
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
2.3、post 表單同步請(qǐng)求
okhttp?發(fā)起post表單格式的數(shù)據(jù)提交,同步請(qǐng)求編程也非常的簡(jiǎn)單,只需要幾行代碼就可以搞定。
案例如下!
String url = "https://www.baidu.com/";
OkHttpClient client = new OkHttpClient();
// 配置 POST + FORM 格式數(shù)據(jù)請(qǐng)求
RequestBody body = new FormBody.Builder()
.add("userName", "zhangsan")
.add("userPwd", "123456")
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 打印返回結(jié)果
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
2.4、post 表單 + 文件上傳,同步請(qǐng)求
如果在發(fā)起表單請(qǐng)求的時(shí)候,還需要上傳文件,該如何實(shí)現(xiàn)呢?
案例如下!
String url = "https://www.baidu.com/";
OkHttpClient client = new OkHttpClient();
// 要上傳的文件
File file = new File("/doc/Downloads/429545913565844e9b26f97dbb57a1c3.jpeg");
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
// 表單 + 文件數(shù)據(jù)提交
RequestBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("userName", "zhangsan")
.addFormDataPart("userPwd", "123456")
.addFormDataPart("userFile", "00.png", fileBody)
.build();
Request request = new Request.Builder()
.url(url)
.post(multipartBody)
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 打印返回結(jié)果
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
2.5、post + json 數(shù)據(jù),同步請(qǐng)求
okhttp?發(fā)起post? + json格式的數(shù)據(jù)提交,同步請(qǐng)求編程也很簡(jiǎn)單。
案例如下!
MediaType contentType = MediaType.get("application/json; charset=utf-8");
String url = "https://www.baidu.com/";
String json = "{}";
OkHttpClient client = new OkHttpClient();
// 配置 POST + JSON 請(qǐng)求
RequestBody body = RequestBody.create(contentType, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 打印返回結(jié)果
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
2.5、文件下載,同步請(qǐng)求
文件下載,通常是get方式請(qǐng)求,只需要在響應(yīng)端使用字節(jié)流接受數(shù)據(jù)即可!
案例如下!
public static void main(String[] args) {
//目標(biāo)存儲(chǔ)文件
String targetFile = "/doc/Downloads/1.png";
//需要下載的原始文件
String url = "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png";
OkHttpClient client = new OkHttpClient();
// 配置GET請(qǐng)求
Request request = new Request.Builder()
.url(url)
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 獲取文件字節(jié)流
byte[] stream = response.body().bytes();
// 寫(xiě)入目標(biāo)文件
writeFile(targetFile, stream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 寫(xiě)入目標(biāo)文件
* @param targetFile
* @param stream
* @throws IOException
*/
private static void writeFile(String targetFile, byte[] stream) throws IOException {
String filePath = StringUtils.substringBeforeLast(targetFile, "/");
Path folderPath = Paths.get(filePath);
if(!Files.exists(folderPath)){
Files.createDirectories(folderPath);
}
Path targetFilePath = Paths.get(targetFile);
if(!Files.exists(targetFilePath)){
Files.write(targetFilePath, stream, StandardOpenOption.CREATE);
}
}
2.6、其他方式的同步請(qǐng)求
在實(shí)際的項(xiàng)目開(kāi)發(fā)中,有的接口需要使用put?或者delete方式請(qǐng)求,應(yīng)該如何處理呢?
put方式請(qǐng)求,案例如下!
// 只需要在 Request 配置類(lèi)中,換成 put 方式即可
Request request = new Request.Builder()
.url(url)
.put(body)
.build();
同樣的,delete方式請(qǐng)求也類(lèi)似,案例如下!
// 只需要在 Request 配置中,換成 delete 方式即可
Request request = new Request.Builder()
.url(url)
.delete(body)
.build();
2.7、自定義添加請(qǐng)求頭部
大部分的時(shí)候,基于安全的考慮,很多時(shí)候我們需要把相關(guān)的鑒權(quán)參數(shù)放在請(qǐng)求頭部,應(yīng)該如何處理呢?
以post? + json格式請(qǐng)求為例,添加頭部請(qǐng)求參數(shù),案例如下!
MediaType contentType = MediaType.get("application/json; charset=utf-8");
String url = "https://www.baidu.com/";
String json = "{}";
OkHttpClient client = new OkHttpClient();
// 配置 header 頭部請(qǐng)求參數(shù)
Headers headers = new Headers.Builder()
.add("token", "11111-22222-333")
.build();
// 配置 POST + JSON 請(qǐng)求
RequestBody body = RequestBody.create(contentType, json);
Request request = new Request.Builder()
.url(url)
.headers(headers)
.post(body)
.build();
// 發(fā)起同步請(qǐng)求
try (Response response = client.newCall(request).execute()){
// 打印返回結(jié)果
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
2.8、發(fā)起異步請(qǐng)求
在上文中我們介紹的都是同步請(qǐng)求,在最開(kāi)始我們也說(shuō)到 OkHttp 不僅支持同步調(diào)用,也異步調(diào)用,那么如何進(jìn)行異步請(qǐng)求編程呢?
其實(shí)操作很簡(jiǎn)單,案例如下!
String url = "https://www.baidu.com/";
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url(url)
.get()
.build();
// 發(fā)起異步請(qǐng)求
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("請(qǐng)求異常 + " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("請(qǐng)求完成,返回結(jié)果:" + response.body().string());
}
});
三、小結(jié)
以上就是小編針對(duì) OkHttp 在使用上,做了一次簡(jiǎn)單的內(nèi)容總結(jié),整體下來(lái),從使用上來(lái)講,api 的操作確實(shí)比 Apache HttpClient 要簡(jiǎn)單很多。
關(guān)于 OkHttp 的內(nèi)容其實(shí)還有很多,比如請(qǐng)求參數(shù)的全局配置、全局?jǐn)r截器、websocket 等功能。