1

I have an app that is the default launcher of Android.

When I reboot phone, app runs, then try to make http call and gets this exception:

HTTP FAILED: java.net.SocketTimeoutException: failed to connect to XXXX (port 80) from localAddress.getAddress() (port 42208) after 30000ms

If I leave the app and return to it, http calls starts to work.

This is my client setup:

val client = OkHttpClient.Builder()
            client.connectTimeout(30, TimeUnit.SECONDS)
            client.readTimeout(30, TimeUnit.SECONDS)
            client.writeTimeout(30, TimeUnit.SECONDS )
            client.retryOnConnectionFailure(true)
            client.connectionPool(ConnectionPool(0, 5, TimeUnit.MINUTES))
            client.protocols(listOf(Protocol.HTTP_1_1))

val retrofit = Retrofit.Builder()
                    .client(client.build())
                    .addCallAdapterFactory(
                            RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(
                            GsonConverterFactory.create())
                    .baseUrl(base_url)
                    .build()

Device: Xiaomi Redmi 8a

OS: MIUI 10.3.3.0 Android 9

Retrofit: 2.7.2

OKHttp: 4.4.0

Itai Spector
  • 652
  • 11
  • 24

2 Answers2

2

I have faced a similar issue. Not sure is this your answer but I hope, it will fix your problem.

If I am wrong you are trying to fetch data using RxJava2. According to this document - error handling documentation. The documentation tells throwable errors doesn’t handle after the observable disposed/terminated

That means when your Single/Observable is called onComplete(), sometimes it carries an error and throws UndeliverableException, SocketTimeoutException or other Exceptions. We faced SocketTimeoutException a lot in the Firebase Crashlytics.

We have handled just add this line in our AppApplication class [onCreate()] -

RxJavaPlugins.setErrorHandler { error: Throwable ->
        Timber.e(error) // you can do some task with the Throwable here if you need
    }

I hope, it will help... :)

sunnat629
  • 131
  • 6
  • Sorry friend, I've tried your suggestion, still no luck, thanks for trying – Itai Spector Mar 04 '20 at 10:31
  • Sorry to hear that. Can you please check this out? I found it in `okhttp` issue - https://github.com/square/okhttp/issues/4671 – sunnat629 Mar 04 '20 at 16:20
  • Been there as well. I tried to make the call just with OkHttp without Retrofit, and still facing this issue. – Itai Spector Mar 04 '20 at 16:26
  • Is your URL localhost? I think, your given URL it's a problem. Can you check other URLs? From Android 8, Cleartext HTTP traffic is not permitted by default. You have to add `android:usesCleartextTraffic="true"` in the `AndroidManifest`. for details: https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted – sunnat629 Mar 04 '20 at 16:51
  • It's not localhost. The thing is, when reopening the app, everything works – Itai Spector Mar 04 '20 at 20:16
0

for me, it was network issue. i use mobile data

  1. there can be network issue.
    a. try to reconnect to network.
    b. check firewall.

  2. there can be server issue.
    a. server could be overloaded, wait for some time.
    b. check for log (bug) report.

sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42