简体   繁体   English

OkHttp设置读取超时。 安卓系统

[英]OkHttp set read timeout. Android

How can I change read timeout for my OkHttpClient not generally but only for single specific request? 如何不一般但仅针对单个特定请求更改OkHttpClient的读取超时?

I tried to manage it by interceptors, but interceptors doesn't provide this functionality. 我试图通过拦截器来管理它,但是拦截器不提供此功能。

you can try 你可以试试

  OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();

Use OkHttpClient.newBuilder() . 使用OkHttpClient.newBuilder() From the official Javadoc : 官方Javadoc

You can customize a shared OkHttpClient instance with newBuilder(). 您可以使用newBuilder()自定义共享的OkHttpClient实例。 This builds a client that shares the same connection pool, thread pools, and configuration. 这将构建共享相同连接池,线程池和配置的客户端。 Use the builder methods to configure the derived client for a specific purpose. 使用构建器方法为特定目的配置派生的客户机。

Their example seems to do exactly what you're trying to do: 他们的示例似乎完全符合您要执行的操作:

OkHttpClient eagerClient = client.newBuilder()
    .readTimeout(500, TimeUnit.MILLISECONDS)
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM