okhttp的简单使用

1.同步get请求

   对于小文档,响应体上的string()方法非常方便和高效。但是,如果响应主体很大(大于1 MB),则应避免string(),因为它会将整个文档加载到内存中。在这种情况下,将主体处理为流。
private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("https://publicobject.com/helloworld.txt")
        .build();

    try (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());
    }
  }

2.异步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 {
       try (ResponseBody responseBody = response.body()) {
         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(responseBody.string());
       }
     }
   });
 }

添加请求头

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();

   try (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"));
   }
 }

3.post String上传

   使用HTTP POST向服务发送请求体。本例将markdown文档发布到web服务器呈现为HTML。由于整个请求体同时位于内存中,因此避免使用此API发布大型(大于1 MB)文档。
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();

   try (Response response = client.newCall(request).execute()) {
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

     System.out.println(response.body().string());
   }

4. post 流上传

   这里,我们将请求体作为一个流发布。此请求体的内容在编写时生成。这个例子直接流进Okio缓冲池。您的程序可能更喜欢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();

   try (Response response = client.newCall(request).execute()) {
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

     System.out.println(response.body().string());
   }
 }

5. post 文件上传

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();

   try (Response response = client.newCall(request).execute()) {
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

     System.out.println(response.body().string());
   }
 }

6. post 上传表单

   使用FormBody。构建器来构建一个类似于HTML 
标记的请求体。名称和值将使用html兼容的表单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();

   try (Response response = client.newCall(request).execute()) {
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

     System.out.println(response.body().string());
   }
 }

7. post 多种请求体上传

MultipartBody.Builder 可以构建与HTML文件上传表单兼容的复杂请求体。复杂请求体的每个部分本身都是一个请求体,并且可以定义自己的头。如果存在,这些头应该描述部件主体,比如它的内容配置。如果内容长度和内容类型头可用,则自动添加它们 。

     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();

   try (Response response = client.newCall(request).execute()) {
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

     System.out.println(response.body().string());
   }
 }

8. response caching

    要缓存响应,您需要一个可以读写的缓存目录,以及缓存大小的限制。缓存目录应该是私有的,不受信任的应用程序不应该能够读取它的内容!
    多个缓存同时访问同一个缓存目录是错误的。大多数应用程序应该只调用new OkHttpClient()一次,用它们的缓存配置它,并在任何地方使用相同的实例。否则,这两个缓存实例将相互践踏,破坏响应缓存,并可能导致程序崩溃。
      Response caching uses HTTP headers for all configuration. You can add request headers like Cache-Control: max-stale=3600 and OkHttp's cache will honor them. Your webserver configures how long responses are cached with its own response headers, like Cache-Control: max-age=9600. There are cache headers to force a cached response, force a network response, or force the network response to be validated with a conditional 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();

   String response1Body;
   try (Response response1 = client.newCall(request).execute()) {
     if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);

     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());
   }

   String response2Body;
   try (Response response2 = client.newCall(request).execute()) {
     if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);

     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));
 }

9. Canceling a Call

      Use Call.cancel() to stop an ongoing call immediately. If a thread is currently writing a request or reading a response, it will receive an IOException. Use this to conserve the network when a call is no longer necessary; for example when your user navigates away from an application. Both synchronous and asynchronous calls can be canceled.
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);

   System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
   try (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);
   }
 }

10. Timeout

Use timeouts to fail a call when its peer is unreachable. Network partitions can be due to client connectivity problems, server availability problems, or anything between. OkHttp supports connect, read, and write timeouts.
 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();

   try (Response response = client.newCall(request).execute()) {
     System.out.println("Response completed: " + response);
   }
 }

你可能感兴趣的:(okhttp的简单使用)