Android:使用 RxJava 和 OkHttp SocketTimeoutException 进行网络调用
我有一个使用 Retrofit/OkHttp 发出的简单 API 请求。
private fun getNext(
nextId: Long
): Observable<NextData> =
userRepo.getNext(nextId)
.map { it.toDomainObject() }
API 回复 200, OK
并且回复速度很快(绝对少于 3 秒)。但是,此调用有时会抛出异常。
java.net.SocketTimeoutException: timeout
我还将 OkHttpClient
配置为具有 callTimeout(15, TimeUnit.SECONDS)
。我真的不明白为什么我会有这样的例外。任何有关解决此问题的想法将不胜感激。
I have a simple API request which I make by using Retrofit/OkHttp.
private fun getNext(
nextId: Long
): Observable<NextData> =
userRepo.getNext(nextId)
.map { it.toDomainObject() }
The API replies 200, OK
and it replies fast (definitely less than 3 seconds). However, this call sometimes throws
java.net.SocketTimeoutException: timeout
I also configured my OkHttpClient
to have callTimeout(15, TimeUnit.SECONDS)
. I really do not understand why I have such an exception. Any ideas about solving this problem would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会用 Kotlin 编写代码,但这也许会对您有所帮助。
我遇到了同样的问题,这在 Java 中对我有帮助:
I don't code in Kotlin but maybe this will help you.
I had same problem and this helped me in Java: