OKHttp

HTTP是现代应用程序网络的方式。这是我们交换数据和媒体的方式。

高效地执行HTTP可以使您的内容加载更快,节省带宽。

OkHttp 是一个具有如下优势的 HTTP 客户端:

  • HTTP/2支持允许对同一主机的所有请求共享一个套接字。

  • 连接池减少请求延迟(如果HTTP/2不可用)。

  • 透明的GZIP压缩下载大小。

  • 响应缓存避免了重复请求的网络。

当网络出现问题时,OkHttp保持不变:它将从常见的连接问题中悄悄地恢复。 如果您的服务有多个IP地址,那么当第一个连接失败时,OkHttp将尝试替换地址。 这对于IPv4+IPv6和冗余数据中心托管的服务是必要的。OkHttp启动具有现代TLS特性的新连接(SNI, ALPN),如果握手失败,返回到TLS 1.0。

使用OkHttp很容易。它的请求/响应API是用流畅的构建器和不可变性设计的。它支持同步阻塞调用和带有回调的异步调用。

OkHttp支持Android 2.3和以上。对于Java,最低要求是1.7。

快速入门

Jar 依赖

  [xml]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.2.0</version> </dependency> <!--Okio用于给OkHttp提供快速的I/O和可调整大小的缓存池。--> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.6.0</version> </dependency> </dependencies>

Get

  [java]
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
import java.io.IOException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * <p> </p> * * <pre> Created: 2018/6/8 上午7:43 </pre> * <pre> Project: tech-validation </pre> * * @author houbinbin * @version 1.0 * @since JDK 1.7 */ public class OkHttpGet { /** * 获取一个 URL 对应的信息 * @param url 网络地址 * @return 请求反馈 * @throws IOException if any */ String get(String url) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } public static void main(String[] args) throws IOException { OkHttpGet example = new OkHttpGet(); String response = example.get("https://raw.github.com/square/okhttp/master/README.md"); System.out.println(response); } }

Post

  [java]
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.IOException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * <p> </p> * * <pre> Created: 2018/6/8 上午7:43 </pre> * <pre> Project: tech-validation </pre> * * @author houbinbin * @version 1.0 * @since JDK 1.7 */ public class OkHttpPost { public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); /** * 获取一个 URL 对应的信息 * @param url 网络地址 * @return 请求反馈 * @throws IOException if any */ private String post(String url, String json) throws IOException { OkHttpClient client = new OkHttpClient(); 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(); } /** * 构建 JSON 数据 * @param player1 用户1 * @param player2 用户2 * @return JSON */ private 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 { OkHttpPost example = new OkHttpPost(); String json = example.bowlingJson("Jesse", "Jake"); String response = example.post("http://www.roundsapp.com/post", json); System.out.println(response); } }

代码地址

okhttp