Android开源项目分析-OkHttp简介

概述

OkHttp是Android和Java应用程序用来处理的HTTP和HTTP/2协议的客户端。HTTP作为当前网络应用中最通用的数据&媒体交换方式,高效的HTTP能够使任务加载更快并节省带宽是各类HTTP客户端需要处理的问题。OkHttp主要支持有以下特征:

  • HTTP/2 支持发往同一主机的请求共享一个socket。
  • 连接池减少了请求延迟(如果HTTP/2不可用)。
  • 透明的GZIP减小了下载数据量大小。
  • 响应缓存阻止了网络的重复请求。

通常OkHttp在网络出现常见的连接问题时能够进行有效的恢复。例如当服务器有多个IP地址时,在第一次请求连接失败后,OkHttp会尝试切换下一个IP地址进行连接。这在同时具备IPV4+IPV6地址或服务处于冗余数据中心的情况下非常有必要。OkHttp初始化新连接时使用最新的TLS特征(SNI,ALPN),如果在握手失败时会回退到TLS 1.0尝试进行连接。

使用OkHttp非常简单。它的请求/响应 API采用流式生成器(fluent builders)和 immutability设计。它同时支持同步阻塞调用和异步回调调用。(关于fluent builders查看Fluent Interface)

OkHttp支持Android2.3及后续版本。JAVA最低版本需求为1.7。

示例

GET方式

示例展示了如何使用OkHttp进行GET请求,它下载URL的对应数据并且以字符串形式打印它的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package okhttp3.guide;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class GetExample {
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
GetExample example = new GetExample();
String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
System.out.println(response);
}
}
This program downloads a URL and print its contents as a string. Full source.
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();
}

POST方式

程序展示了如何post数据到服务器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package okhttp3.guide;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class PostExample {
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();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
String bowlingJson(String player1, String player2) {
return "{'winCondition':'HIGH_SCORE',"
+ "'name':'Bowling',"
+ "'round':4,"
+ "'lastSaved':1367702411696,"
+ "'dateStarted':1367702378785,"
+ "'players':["
+ "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
+ "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
+ "]}";
}
public static void main(String[] args) throws IOException {
PostExample example = new PostExample();
String json = example.bowlingJson("Jesse", "Jake");
String response = example.post("http://www.roundsapp.com/post", json);
System.out.println(response);
}
}

依赖

OkHttp依赖Okio

相关链接

【1】https://github.com/square/okhttp