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.

DeePanShu
  • 1,236
  • 10
  • 23

2 Answers2

1

This is some 4G network issues here Like in Vodafone Network. The connection time in this network for TCP connecting which is connectTimeout is always connecting so the api is not able connect before 2 mins because timeout is mentioned of 2 mins

So, I solve this by reducing the TCP connecting time which is connectTimeout to 1sec, here is the code :

OkHttpClient.Builder()
        .addInterceptor(HeaderIntercepter())
        .callTimeout(2, TimeUnit.MINUTES)
        .connectTimeout(1, TimeUnit.SECONDS)
        .readTimeout(1, TimeUnit.MINUTES)
        .writeTimeout(1, TimeUnit.MINUTES)
        .build()

If you api or url not needs the TCP connection between data sending and receiving through retrofit then you can do like this, working fine for me now.

DeePanShu
  • 1,236
  • 10
  • 23
0

Add network-security-config to res/xml folder

<?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>

Add a line in manifest file application tag

android:networkSecurityConfig="@xml/network_security_config"

And make sure you give internet permission in manifest file

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

Also add this in manifest file

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

add useLibrary 'org.apache.http.legacy' in your app build 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'
}
Dinesh
  • 1,410
  • 2
  • 16
  • 29