android - Retrofit 2.6.2 is not working on Vodafone mobile data but works fine on WiFi
问题描述
Please help me Some of my app users are complaining or giving feedback that their app doesn't work on mobile data (Vodafone 4G) but works on wifi.
I am using Retrofit-2.6.2 and okhttp3 - 4.2.2.
Retrofit.Builder()
.baseUrl(baseurl)
.addConverterFactory(GsonConverterFactory.create())
.client(getClient())
.build()
fun getClient(): OkHttpClient {
return OkHttpClient.Builder().addInterceptor(HeaderIntercepter())
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.connectTimeout(2, TimeUnit.MINUTES)
.build()
}
APIs are made on Amazon server using http and https both.
What are the things missing from my side please give me solution?
Thanks in Advance.
解决方案
这是一些4G 网络问题,就像在沃达丰网络中一样。该网络中用于TCP 连接的连接时间(即connectTimeout )始终处于连接状态,因此 api 在2 分钟之前无法连接,因为提到了2 分钟的超时
所以,我通过将 TCP 连接时间(connectTimeout)减少到 1sec 来解决这个问题,代码如下:
OkHttpClient.Builder()
.addInterceptor(HeaderIntercepter())
.callTimeout(2, TimeUnit.MINUTES)
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
.build()
如果您的 api 或 url 不需要通过改造在数据发送和接收之间建立 TCP 连接,那么您可以这样做,现在对我来说工作正常。
推荐阅读
- reactjs - 从 firebase 成功删除项目后出现错误
- javascript - windows.location 不适用于我重定向到另一个 html 文件
- css - 我可以使用 perl 的 Parse::RecDescent 就地解析文件吗?
- java - 如何修复“FFmpeg 无法就地编辑现有文件。”-Java 中的问题
- php - 意外行为 PHP float 到 int 转换
- pytorch - 为什么这个 Pytorch 官方教程中没有 .train() 方法?
- c# - 如何打印列表值
- android - (可缩放)图形而不是用于设计目的的文本
- javascript - 将额外的道具传递给 Div - React
- html - 我们可以在不下载包装的情况下编辑引导 css 样式吗?