美文网首页
《okhttp文档翻译第二篇:常用方法解析》

《okhttp文档翻译第二篇:常用方法解析》

作者: zzc不是自助餐 | 来源:发表于2019-03-24 13:16 被阅读0次

    关于对okhttp的介绍请看第一章,本文来自对okhttp中wiki的介绍

    2、okhttp的常用场景介绍

    2.1、同步get请求

    同步请求会在execute()方法所在的线程中执行,为了避免在主线程发起网络请求,需要在子线程中执行该execute()方法。通过string()方法获取响应体(response body)的值是非常方便和高效的,但是由于string()方法会将整个响应体加载到内存中,当响应体太大时(超过1M)建议使用流的方式获取数据。代码如下:

    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));
          }
          //注意string()仍有可能阻塞线程
          System.out.println(response.body().string());
        }
      }
    

    2.2、异步get请求

    通过enqueue()方法来执行异步请求,okhttp默认会开启一个工作线程来执行该网络请求。同时callback的返回方法也是在子线程调用的,所以也不能再次进行更新UI。代码如下:

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

    2.3、遍历头部(headers)

    headers的数据结构类似于map<String, String>,不同点是headers允许一个键(key)可以对应多个值(value)。

    • 添加头部:当使用header(name, value)方法添加请求头时,会保证键值对一一对应,如果已存在该name对应的值,则会移除它并将新值(value)添加进去。当使用addHeader(name, value)添加请求头时,不会保证键值对一一对应,新值会和旧值一起存在。
    • 获取头部:header(name)返回最后添加进去的值,headers(name)返回该键对应的值的集合。代码如下:
    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"));
        }
      }
    

    2.4、post提交String

    http通过post请求将请求体(request body)发送到服务端。同样,由于请求体会完整的加载到内存中,为了避免内存溢出,大尺寸的字符串(超过1M)建议使用流的方式提交。代码如下:

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

    2.5、 post提交流

    对于大的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 {
        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());
        }
      }
    

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

    file文件的提交主要通过RequestBody的create方法构造一个requestBody对象。file以字节流的形式有sink写入提交(类似于上面的post提交流)。源码如下:

    public static RequestBody create(final @Nullable MediaType contentType, final byte[] content,
          final int offset, final int byteCount) {
        if (content == null) throw new NullPointerException("content == null");
        Util.checkOffsetAndCount(content.length, offset, byteCount);
        return new RequestBody() {
          @Override public @Nullable MediaType contentType() {
            return contentType;
          }
    
          @Override public long contentLength() {
            return byteCount;
          }
    
          @Override public void writeTo(BufferedSink sink) throws IOException {
            sink.write(content, offset, byteCount);
          }
        };
      }
    

    2.7、post提交键值对

    代码如下:

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

    2.8、post提交多类型数据

    通过multipartBody构建requestBody,代码如下:

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

    2.9、使用缓存

    首先应创建一个私有的缓存文件目录,不被信任的应用应该不能读取它。
    不同的cache访问同一个缓存目录是错误的,我们应该保证okhttpClient只创建一次,并在创建okhttpClient的时候设置缓存。然后在应用的任何地方都应该使用同一个okhttpClient。
    http通过请求头来设置缓存。例如在请求头(request header)中添加ache-Control: max-stale=3600,okhttp会响应这个请求头,给当前请求的缓存设置过期时间3600秒。服务端通过设置响应头(response header)也可以为客户端请求配置缓存,例如Cache-Control: max-age=9600。
    在request.builder中设置CacheControl为CacheControl.FORCE_NETWORK,响应将只从网络获取。
    在request.builder中设置CacheControl为CacheControl.FORCE_CACHE,响应将只从缓存中获取。
    代码如下:

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

    2.10、取消请求

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

    2.11、设置超时

    使用超时策略来使一个请求变得失败当这个连接是不可到达的,网络错误、服务器错误都有可能导致客户端连接问题。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();
    
        try (Response response = client.newCall(request).execute()) {
          System.out.println("Response completed: " + response);
        }
      }
    

    2.12、配置call

    所有http客户端的配置都是在OKhttpClient中,如代理设置、超时策略、缓存策略。当你需要对某个请求改变这个配置的时候可以通过OkHttpClient.newBuilder()来实现,这将返回与原始client共享连接池、调度器、和原始配置的builder。代码如下:

     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();
    
        // Copy to customize OkHttp for this request.
        OkHttpClient client1 = client.newBuilder()
            .readTimeout(500, TimeUnit.MILLISECONDS)
            .build();
        try (Response response = client1.newCall(request).execute()) {
          System.out.println("Response 1 succeeded: " + response);
        } catch (IOException e) {
          System.out.println("Response 1 failed: " + e);
        }
    
        // Copy to customize OkHttp for this request.
        OkHttpClient client2 = client.newBuilder()
            .readTimeout(3000, TimeUnit.MILLISECONDS)
            .build();
        try (Response response = client2.newCall(request).execute()) {
          System.out.println("Response 2 succeeded: " + response);
        } catch (IOException e) {
          System.out.println("Response 2 failed: " + e);
        }
      }
    

    2.13、处理授权信息

    okhttp会自动重试一个未授权的请求。当服务器返回一个401未授权的状态码,okhttp将寻求Authenticator 提供一个证书。新的请求应该包含这个缺失的证书,如果没有可用的证书,okhttp将返回null,并且跳过重试。代码如下:

    private final OkHttpClient client;
    
      public Authenticate() {
        client = new OkHttpClient.Builder()
            .authenticator(new Authenticator() {
              @Override public Request authenticate(Route route, Response response) throws IOException {
                if (response.request().header("Authorization") != null) {
                  return null; // Give up, we've already attempted to authenticate.
                }
    
                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();
    
        try (Response response = client.newCall(request).execute()) {
          if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
          System.out.println(response.body().string());
        }
      }
    To avoid making many retries when authentication isn't working, you can return null to give up. For example, you may want to skip the retry when these exact credentials have already been attempted:
    
      if (credential.equals(response.request().header("Authorization"))) {
        return null; // If we already failed with these credentials, don't retry.
       }
    

    为了避免多次重试授权都无效,你可以在实现中返回null来放弃重试。例如你应该跳过重新请求当你提供的证书仍然被拒绝的时候。代码如下:

    if (credential.equals(response.request().header("Authorization"))) {
        return null; // If we already failed with these credentials, don't retry.
       }
    

    又或者重试次数已经超过了APP限制你也应该取消重试。代码如下:

     if (responseCount(response) >= 3) {
        return null; // If we've failed 3 times, give up.
      }
    
    private int responseCount(Response response) {
        int result = 1;
        while ((response = response.priorResponse()) != null) {
          result++;
        }
        return result;
      }
    

    相关文章

      网友评论

          本文标题:《okhttp文档翻译第二篇:常用方法解析》

          本文链接:https://www.haomeiwen.com/subject/zxzyvqtx.html