首页 > 解决方案 > 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.

标签: androidandroid-studionetworkingretrofitretrofit2.6

解决方案


这是一些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 连接,那么您可以这样做,现在对我来说工作正常。


推荐阅读