我想知道我应该为我的改造客户设置多少秒。
- 我应该使用多少秒作为默认超时?
- OkHttp/Retrofit 的默认超时时间是多少,我们应该让默认值吗?
最佳答案
- 没有什么神奇的值(value),取决于您对后端的期望。如果有人告诉您 5s 是一个很好的值(value),并且您在最大加载时间的一个端点上平均有 8s,那么 8s 不适合您。作为一般值,我发现 10 秒以下被认为是短的,而 10 到 20 秒之间是常见的。
- 截至 OkHttp 3.4.1 和 Retrofit 2.1.0 ,OkHttp 的默认值为 10 秒。改造依赖于 OkHttp 默认值。
改造代码 fragment :(如果您不提供 OkHttpClient):
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
OkHttp 代码 fragment :
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
- 我唯一可以访问的 Google 应用是 Google IO应用程序。
他们使用不同的值。例如 feedback他们使用的相关:
public class FeedbackConstants{
public static final int FEEDBACK_CONNECT_TIMEOUT_MS = 15000;
public static final int FEEDBACK_READ_TIMEOUT_MS = 15000;
public static final int GOOGLE_API_CLIENT_CONNECTION_TIMEOUT_S = 10;
}
他们正在使用 Volley 而你 can take a look那里也有一些超时。是的,它们看起来很短。
/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;
在 different http client 中他们会为您提供一些线索,说明他们认为什么是短暂且合理的短暂超时。
/**
* Default 2s, deliberately short. If you need longer, you should be using
* {@link AsyncHttpClient} instead.
*/
protected int connectionTimeout = 2000;
/**
* Default 8s, reasonably short if accidentally called from the UI thread.
*/
protected int readTimeout = 8000;
关于android - OkHttp/Retrofit 默认超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30767460/