OkHttp/Retrofit默认超时 [英] OkHttp/Retrofit default timeout
本文介绍了OkHttp/Retrofit默认超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道应该为我的改造客户设置多少秒.
I was wondering how many seconds should I set to my retrofit client.
- 我应该使用几秒钟作为默认超时时间?
- OkHttp/Retrofit的默认超时是多少,我们应该让默认值吗?
推荐答案
- 没有神奇的价值,取决于您对后端的期望.如果有人告诉您5s是一个很好的值,并且在最大负载时间下,您在一个端点上的平均值为8s,则8s对您不起作用.作为一般值,我发现低于10s被认为是短时间,而介于10s和20s之间是常见的情况.
- 截至 OkHttp 3.4.1 和 Retrofit 2.1.0 ,OkHttp的默认值为10秒.翻新依赖于OkHttp默认值.
改造代码段:(如果您不提供OkHttpClient的话):
Retrofit code snippet: (if you don't provide an OkHttpClient):
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
OkHttp代码段:
OkHttp code snippet:
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
- 我唯一有权访问的Google应用是 Google IO 应用.
他们使用不同的值.例如反馈相关:
They are using different values. For example for feedback related they use:
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;
}
They are using Volley and you can take a look at some timeouts there as well. And yes they look short.
/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;
In a different http client they give you some clues about what they consider is a short and reasonable short timeout.
/**
* 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;
这篇关于OkHttp/Retrofit默认超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文