OkHttp之示例
我们写了一些方法来示例如果使用OkHttp来解决常见问题。通读它们了解它们是如何一起工作的。随意地进行复制、粘贴,这是它们存在的目的。
同步Get请求
下载一个文件,打印它的头,并将其响应主体以字符串形式打印。
作用在响应主体上的string()方法对于小文档来说是方便和高效的。但是如果响应主体比较大(大于1MB),避免使用string(),因为它会加载整个文档到内存中。在这种情况下,优先使用stream来处理主体。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
异步Get请求
在一个工作线程下载一个文件,当响应可读时获取回调。这个回调将在响应头准备好时执行。读取响应主体可能仍然阻塞。OkHttp当前没有提供异步API来局部地接收响应主体。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
});
}
访问Header
典型的HTTP头工作起来像一个Map< String, String >,每一个字段有一个值或没有值。但是有一些头允许多个值,像Guava的Multimap。对于一个HTTP响应来应用多个Vary头是合法的并且常见的。OkHttp的API试图兼容这些情况。
当写请求头时,使用header(name, value)的方式来设置唯一出现的键值。如果已有值,会在新值添加前移除已有值。使用addHeader(name, value)来添加一个头而不移除已经存在的头。
当读取一个响应头时,使用header(name)来返回最后一次出现的键值对。通常这是唯一出现的键值对。如果不存在值,header(name)会返回null。使用headers(name)来用一个list读取一个字段的所有值。
使用支持按索引访问的Headers类来访问所有的头。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("https://api.github.com/repos/square/okhttp/issues")
.header("User-Agent", "OkHttp Headers.java")
.addHeader("Accept", "application/json; q=0.5")
.addHeader("Accept", "application/vnd.github.v3+json")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println("Server: " + response.header("Server"));
System.out.println("Date: " + response.header("Date"));
System.out.println("Vary: " + response.headers("Vary"));
}
上传字符串
使用HTTP POST来发送请求主体到服务器。这个例子上传了一个markdown文档到一个用HTML渲染markdown的服务器中。因为整个请求主体同时存在内存中,避免使用这个API上传大的文档(大于1MB)。
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
上传流
这里以stream的形式上传请求主体。请求主体的内容如它写入的进行生成。这个示例stream直接放入到了Okio缓存sink中。你的程序可能需要一个OutputStream,你可以从BufferedSink.outputStream()中获取。
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
RequestBody requestBody = new RequestBody() {
@Override public MediaType contentType() {
return MEDIA_TYPE_MARKDOWN;
}
@Override public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("Numbers\n");
sink.writeUtf8("-------\n");
for (int i = 2; i <= 997; i++) {
sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
}
}
private String factor(int n) {
for (int i = 2; i < n; i++) {
int x = n / i;
if (x * i == n) return factor(x) + " × " + i;
}
return Integer.toString(n);
}
};
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
上传文件
使用文件作为请求主体很容易。
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
File file = new File("README.md");
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
上传表格参数
使用FormBody.Builder来构建一个工作起来像HTML< form >标签的请求主体。键值对会使用一个兼容HTML form的URL编码进行编码。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
Request request = new Request.Builder()
.url("https://en.wikipedia.org/w/index.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
上传多部分请求
MultipartBody.Builder可以构造复杂的请求主体与HTML文件上传表单兼容。multipart请求主体的每部分本身就是一个请求主体,可以定义它自己的头。如果存在自己的头,那么这些头应该描述部分主体,例如它的Content-Disposition。Content-Length和Content-Type会在其可用时自动添加。
private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
.build();
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.url("https://api.imgur.com/3/image")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
使用Gson解析Json响应
Gson是一个便利API来实现JSON和Java对象之间的转化。这里我们使用它来解码来自GitHub API的JSON响应。
ResponseBody.charStream()使用响应头Content-Type来确定在解码响应主体时使用哪个字符集。如果没有指定字符集,则默认使用UTF-8.
private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson();
public void run() throws Exception {
Request request = new Request.Builder()
.url("https://api.github.com/gists/c2a7c39532239ff261be")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue().content);
}
}
static class Gist {
Map<String, GistFile> files;
}
static class GistFile {
String content;
}
响应缓存
要缓存响应,你需要一个进行读取和写入的缓存目录,以及一个缓存大小的限制。缓存目录应该是私有的,且不被信任的应用不能够读取它的内容。
让多个缓存同时访问相同的混存目录是错误的。大多数应用应该只调用一次new OkHttpClient(),配置它们的缓存,并在所有地方使用相同的实例。否则两个缓存实例会相互进行干涉,腐朽响应缓存,有可能造成你的程序崩溃。
响应缓存使用HTTP头进行所有配置。你可以添加像Cache-Control:max-stale=3600这样的请求头并且OkHttp的缓存会尊重它们。你的服务器使用自己的响应头像Cache-Control:max-age=9600来配置响应缓存多久。这里有缓存头来强制一个缓存响应,强制一个网络响应,强制使用一个条件的GET来验证网络响应。
private final OkHttpClient client;
public CacheResponse(File cacheDirectory) throws Exception {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(cacheDirectory, cacheSize);
client = new OkHttpClient.Builder()
.cache(cache)
.build();
}
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
Response response1 = client.newCall(request).execute();
if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);
String response1Body = response1.body().string();
System.out.println("Response 1 response: " + response1);
System.out.println("Response 1 cache response: " + response1.cacheResponse());
System.out.println("Response 1 network response: " + response1.networkResponse());
Response response2 = client.newCall(request).execute();
if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);
String response2Body = response2.body().string();
System.out.println("Response 2 response: " + response2);
System.out.println("Response 2 cache response: " + response2.cacheResponse());
System.out.println("Response 2 network response: " + response2.networkResponse());
System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body));
}
使用CacheControl.FORCE_NETWORK来阻止响应使用缓存。使用CacheContril.FORCE_CACHE来阻止使用网络。注意:如果你使用FORCE_CACHE且响应需要网络,OkHttp会返回一个504 Unsatisfiable Request响应。
取消调用
使用Call.cancel()来立即停止一个正在进行的调用。如果一个线程正在写请求或读响应,它会接收到一个IOException。当一个调用不再需要时,使用这个来节省网络,例如当用户从应用离开。同步和异步调用都可以取消。
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
.build();
final long startNanos = System.nanoTime();
final Call call = client.newCall(request);
// Schedule a job to cancel the call in 1 second.
executor.schedule(new Runnable() {
@Override public void run() {
System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
call.cancel();
System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
}
}, 1, TimeUnit.SECONDS);
try {
System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
Response response = call.execute();
System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
(System.nanoTime() - startNanos) / 1e9f, response);
} catch (IOException e) {
System.out.printf("%.2f Call failed as expected: %s%n",
(System.nanoTime() - startNanos) / 1e9f, e);
}
}
超时
使用超时来使调用在当另一端没有到达时失败。网络部分可能是由于连接问题,服务器可用性问题或者其他。OkHttp支持连接、读取和写入超时。
private final OkHttpClient client;
public ConfigureTimeouts() throws Exception {
client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
}
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
.build();
Response response = client.newCall(request).execute();
System.out.println("Response completed: " + response);
}
单独配置调用
所有HTTP client配置都存在OkHttpClient中,包括代理设置,超时和缓存。当你需要改变一个单独call的配置时,调用OkHttpClient.newBuilder()。这个会返回一个builder,与原始的client共享下共同的连接池,调度器和配置。在下面的例子中,我们让一个请求有500ms的超时而另一个有3000ms的超时。
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://httpbin.org/delay/1") // This URL is served with a 1 second delay.
.build();
try {
// Copy to customize OkHttp for this request.
OkHttpClient copy = client.newBuilder()
.readTimeout(500, TimeUnit.MILLISECONDS)
.build();
Response response = copy.newCall(request).execute();
System.out.println("Response 1 succeeded: " + response);
} catch (IOException e) {
System.out.println("Response 1 failed: " + e);
}
try {
// Copy to customize OkHttp for this request.
OkHttpClient copy = client.newBuilder()
.readTimeout(3000, TimeUnit.MILLISECONDS)
.build();
Response response = copy.newCall(request).execute();
System.out.println("Response 2 succeeded: " + response);
} catch (IOException e) {
System.out.println("Response 2 failed: " + e);
}
}
处理认证
OkHttp会自动重试未认证请求。当一个响应为401 Not Authorized时,会要求Authenticator来应用证书。Authenticator的实现应该构建一个包含缺失证书的新请求。如果没有证书可用,返回null来跳过重试。
使用Response.challenges()来获取所有认证挑战的模式和领域。当完成一个Basic挑战时,使用Credentials.basic(username,password)来编码请求头。
private final OkHttpClient client;
public Authenticate() {
client = new OkHttpClient.Builder()
.authenticator(new Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
System.out.println("Authenticating for response: " + response);
System.out.println("Challenges: " + response.challenges());
String credential = Credentials.basic("jesse", "password1");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
}
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/secrets/hellosecret.txt")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
为了避免当认证无法工作时过多的尝试,你可以返回null来放弃。例如,当这些明确的证书已经尝试过了,你可能想跳过。
if (credential.equals(response.request().header("Authorization"))) {
return null; // If we already failed with these credentials, don't retry.
}
你可能也想在达到应用定义的尝试限制次数时跳过尝试:
if (respondseCount(response) >= 3) {
return null; // If we've failed 3 times, give up.
}
上面的代码依赖这个responseCount()方法:
private int responseCount(Response response) {
int result = 1;
while ((response = response.priorResponse()) != null) {
result++;
}
return result;
}
原文链接:
https://github.com/square/okhttp/wiki/Recipes
OkHttp官方文档系列文章: