1

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.

4

2 回答 2

1

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

于 2020-06-09T07:38:46.050 回答
0

添加network-security-config到 res/xml 文件夹

<?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
      <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
        <certificates src="system" />
       </trust-anchors>
      </base-config>
    </network-security-config>

manifest在文件application标签中添加一行

android:networkSecurityConfig="@xml/network_security_config"

并确保您在文件中授予internet权限manifest

<uses-permission android:name="android.permission.INTERNET" />

manifest还要在文件中添加这个

 <uses-library
        android:name="org.apache.http.legacy"
        android:required="false"/>

添加useLibrary 'org.apache.http.legacy'您的应用程序构建 gradle

android {
compileSdkVersion 28
defaultConfig {
    applicationId "your_application_id"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    useLibrary 'org.apache.http.legacy'
}
于 2020-04-20T09:23:47.480 回答