为什么retryOnConnectionFailure(true)是OkHttp错误的解决方案:java.io.IOException:意外的流结束

为什么retryOnConnectionFailure(true)是OkHttp错误的解决方案:java.io.IOException:意外的流结束,java,android,retrofit,okhttp,Java,Android,Retrofit,Okhttp,当我发现这个错误时,我已经犯了很长一段时间了。 使用斯旺克耶西提供的解决方案后,错误消失。 我似乎无法理解为什么这是一个解决方案。我在网上找不到这样的东西 解释此方法解决错误的原因 OkHttp文档说: 重新连接故障 将此客户端配置为在出现连接问题时重试或不重试 遇到。默认情况下,此客户端将从 以下问题: 无法访问的IP地址。如果URL的主机有多个IP地址 地址,无法访问任何单个IP地址不会使 总体要求。这可以提高多宿网络的可用性 服务 陈旧的池连接。ConnectionPool将套接字重新用于

当我发现这个错误时,我已经犯了很长一段时间了。 使用斯旺克耶西提供的解决方案后,错误消失。 我似乎无法理解为什么这是一个解决方案。我在网上找不到这样的东西 解释此方法解决错误的原因

OkHttp文档说:

重新连接故障 将此客户端配置为在出现连接问题时重试或不重试 遇到。默认情况下,此客户端将从 以下问题:

无法访问的IP地址。如果URL的主机有多个IP地址 地址,无法访问任何单个IP地址不会使 总体要求。这可以提高多宿网络的可用性 服务

陈旧的池连接。ConnectionPool将套接字重新用于 减少请求延迟,但这些连接偶尔会延长时间 出去

无法访问的代理服务器。ProxySelector可用于按顺序尝试多个代理服务器,最终返回到一个代理服务器 直接连接

上述情况是可以理解的,但它不能证明这是错误的解决方案


提前感谢。

此标志允许OkHttpClient在某些条件为真时重试多次请求,这意味着已知该请求是安全的。如果没有此标志,客户机将立即返回错误,以便客户机在重试时进行dcide

  private fun isRecoverable(e: IOException, requestSendStarted: Boolean): Boolean {
    // If there was a protocol problem, don't recover.
    if (e is ProtocolException) {
      return false
    }

    // If there was an interruption don't recover, but if there was a timeout connecting to a route
    // we should try the next route (if there is one).
    if (e is InterruptedIOException) {
      return e is SocketTimeoutException && !requestSendStarted
    }

    // Look for known client-side or negotiation errors that are unlikely to be fixed by trying
    // again with a different route.
    if (e is SSLHandshakeException) {
      // If the problem was a CertificateException from the X509TrustManager,
      // do not retry.
      if (e.cause is CertificateException) {
        return false
      }
    }
    if (e is SSLPeerUnverifiedException) {
      // e.g. a certificate pinning error.
      return false
    }
    // An example of one we might want to retry with a different route is a problem connecting to a
    // proxy and would manifest as a standard IOException. Unless it is one we know we should not
    // retry, we return true and try a new route.
    return true
  }
在最简单的情况下,如果我们没有开始发送请求,那么我们知道重试一定是安全的。类似地,某些响应代码(如408)指示服务器尚未开始任何工作,因此我们可以重试