官方用一句话进行了介绍:
An HTTP & HTTP/2 client for Android and Java applications.
一、概述
HTTP
是互联网上应用最为广泛的一种网络协议,可用于传输数据和媒体
高效使用HTTP不仅让内容加载更快,且节省带宽
对HTTP 不熟可以看之前的一篇文章:HTTP,请求消息,响应消息,Cookie(详细总结)
-
OkHttp是从设计之初就是一款高效的 HTTP client
-
HTTP/2
允许同一host
的所有请求共享socket
-
连接池降低了请求延迟 (if
HTTP/2
isn’t available) -
GZIP
缩减了下载的大小 -
响应缓存完全避免网络重复请求
-
OkHttp
会确保网络的连接,它会悄悄地恢复一些常见的连接问题 -
如果你的 service 有多个IP地址且第一个连接失败,OkHttp将尝试备用地址。
-
This is necessary for IPv4+IPv6 and for services hosted in redundant data centers.
-
OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.
-
OkHttp 的易用性
-
Its request/response API is designed with fluent builders and immutability.
-
它同时支持同步阻塞调用和回调的异步调用
OkHttp支持Android 2.3及以上。对于Java,最低要求是1.7
二、使用例子
1. GET A URL
下面这个程序下载一个URL,并以字符串打印出其内容:
// 关键代码
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();
}
// 完整代码
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();
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);
}
}
2. POST TO A SERVER
提交数据到服务端
// 关键代码
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();
}
// 完整代码
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();
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);
}
}
三、ja包下载或引用
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
</dependency>
- GRADLE
compile 'com.squareup.okhttp3:okhttp:3.2.0'
参考文章:
[1] 官方原文:OkHttp
网友评论
请教下,service 如何配置多个IP地址啊,谢了