Tuesday, 31 January 2023

Fatal Exception: java.net.SocketTimeoutException on Android 10 and above

I'm getting this crash on my app

(Platform.kt line 128 okhttp3.internal.platform.Platform.connectSocket)
Fatal Exception: java.net.SocketTimeoutException failed to connect to domain/IP (port 443) from /Another IP (port 37062) after 120000ms: isConnected failed: ETIMEDOUT (Connection timed out)

Crash details on firebase crashlytics

libcore.io.IoBridge.isConnected (IoBridge.java:343)


java.net.Socket.connect (Socket.java:646)
okhttp3.internal.platform.Platform.connectSocket (Platform.kt:128)
okhttp3.internal.connection.ConnectPlan.connectSocket (ConnectPlan.kt:246)
okhttp3.internal.connection.ConnectPlan.connectTcp (ConnectPlan.kt:128)
okhttp3.internal.connection.SequentialExchangeFinder.find (SequentialExchangeFinder.kt:41)
okhttp3.internal.connection.RealCall.initExchange$okhttp (RealCall.kt:267)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.kt:32)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:96)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:75)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
co.bosta.bosta_star_app.di.NetworkModuleKt$networkModule$1$invoke$provideOkHttpClient$$inlined$-addInterceptor$1.intercept (OkHttpClient.kt:1156)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.logging.HttpLoggingInterceptor.intercept (HttpLoggingInterceptor.kt:155)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:205)
okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:533)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1137)

java.lang.Thread.run (Thread.java:1012)
Caused by android.system.ErrnoException
isConnected failed: ETIMEDOUT (Connection timed out)
libcore.io.IoBridge.isConnected (IoBridge.java:334)

java.net.Socket.connect (Socket.java:646)
**okhttp3.internal.platform.Platform.connectSocket (Platform.kt:128)**
okhttp3.internal.connection.ConnectPlan.connectSocket (ConnectPlan.kt:246)
okhttp3.internal.connection.ConnectPlan.connectTcp (ConnectPlan.kt:128)
okhttp3.internal.connection.SequentialExchangeFinder.find (SequentialExchangeFinder.kt:41)
okhttp3.internal.connection.RealCall.initExchange$okhttp (RealCall.kt:267)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.kt:32)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:96)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:75)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
co.bosta.bosta_star_app.di.NetworkModuleKt$networkModule$1$invoke$provideOkHttpClient$$inlined$-addInterceptor$1.intercept (OkHttpClient.kt:1156)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.logging.HttpLoggingInterceptor.intercept (HttpLoggingInterceptor.kt:155)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:205)
okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:533)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1137)

java.lang.Thread.run (Thread.java:1012)

Also getting these crashes which also related to server connection

Dns.kt line 49 okhttp3.Dns$Companion$DnsSystem.lookup Fatal Exception: java.net.UnknownHostException Unable to resolve host "host.com": No address associated with hostname

Http2Stream.kt line 675 okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException Fatal Exception: java.net.SocketTimeoutException timeout

ConnectPlan.kt line 315 Fatal Exception: javax.net.ssl.SSLHandshakeException SSL handshake aborted: ssl=0x75dd3e3848: I/O error during system call, Software caused connection abort

JvmOkio.kt line 94 okio.InputStreamSource.read Fatal Exception: java.net.SocketException Software caused connection abort

I found as some answers suggested I must add this to my request but I still getting the crash after that

        .connectTimeout(120, TimeUnit.SECONDS)
        .readTimeout(120, TimeUnit.SECONDS)
        .writeTimeout(120, TimeUnit.SECONDS)

Note: I check interent connection before making an API request using this method

  fun isConnected(): Boolean {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val networkInfo=connectivityManager.activeNetworkInfo
        val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
        if (capabilities != null &&  capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            when {
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
                    return true
                }
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
                    return true
                }
                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> {
                    return true
                }
            }
        }
    } else {
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
            return true
        }
    }
    return false
}


from Fatal Exception: java.net.SocketTimeoutException on Android 10 and above

No comments:

Post a Comment