android - OkHttp/Retrofit 默认超时

标签 android timeout httprequest retrofit okhttp

我想知道我应该为我的改造客户设置多少秒。

  1. 我应该使用多少秒作为默认超时?
  2. OkHttp/Retrofit 的默认超时时间是多少,我们应该让默认值吗?

最佳答案

  1. 没有什么神奇的值(value),取决于您对后端的期望。如果有人告诉您 5s 是一个很好的值(value),并且您在最大加载时间的一个端点上平均有 8s,那么 8s 不适合您。作为一般值,我发现 10 秒以下被认为是短的,而 10 到 20 秒之间是常见的。

  1. 截至 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;

  1. 我唯一可以访问的 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/

相关文章:

c# - 在 Xamarin.Forms 中创建默认短信应用程序

javascript - 如何添加超时类

javascript - transformRequest 仅针对 angularJS 中的特定请求

php - 为 http 请求准备好 ssl 证书

android - 圆形 ImageView 显示方形图像

Android 蓝牙 l2cap 套接字绑定(bind)()

c# - 查询在 NHibernate 中出现超时错误,但在 SQL Server 中却没有

javascript - 使用事件驱动超时清除 Rx.Observable bufferCount?

http - 构成 HTTP 请求(和响应)的字母是否采用 ASCII 格式?

android - 如何通过任何文件资源管理器更改图像背景