Retrofit和OkHttp的java.net.SocketTimeoutException错误

时间:2018-06-20 09:49:48

标签: android retrofit okhttp3

正如标题所述,尝试访问远程数据库时遇到此错误。这是我的崩溃日志的一部分:

retrofit.RetrofitError: failed to connect to xx.xx.xx.xx/xx.xx.xx.x (port 80) from /xx.xx.xx.xx (port 52908) after 15000ms

Caused by: java.net.SocketTimeoutException: failed to connect to xx.xx.xx.xx/xx.xx.xx.x (port 80) from /xx.xx.xx.xx (port 52908) after 15000ms

该应用程序运行良好,因为我已经在各种设备上对其进行了测试,并且服务器已启动并正在运行。

一种解决方法是增加timeout,但是还有其他方法可以解决此问题吗?我阅读的所有帖子都提到了这一点,只建议设置较高的timeout。还是应该设置一个发生此问题的用例,并提醒用户检查其连接?

任何建议都值得赞赏

注意:

    我使用的
  • 改造版本是:1.9.0
  • OkHttp3版本为3.0.1

1 个答案:

答案 0 :(得分:1)

您很有可能会遇到此异常,因为您正尝试从请求中读取数据,但该数据超过了默认超时值

这是网络连接问题,或者您的后端api由于某种原因花费的时间太长。因此,除了增加默认超时之外,没有其他方法可以从客户端进行修复

这可能是

  • 连接超时
  • 读取超时
  • 写入超时

确定您的情况下发生的超时类型,并将客户端附加到改造中

Show Exception Message

@Override
public void onFailure(Call<ResponseType> call, Throwable t) {
    if(t instanceof SocketTimeoutException){
       String message = "Socket Time out!!";
    }
}
相关问题