Fix SocketTimeoutException on Retrofit with Coroutine

Caused by java.net.SocketTimeoutException
failed to connect to xxxxxxx/xxx.xxx.xxx.xxx (port xxx) from /xxx.xxx.xxx.xxx (port xxx) after 10000ms

Coroutine Catch Handler Scenarios

public fun handleCoroutineException(
context: CoroutineContext,
exception: Throwable) {
// Invoke an exception handler from the context if present
try {
context[CoroutineExceptionHandler]?.let {
it.handleException(context, exception)
return
}
} catch (t: Throwable) {
handleCoroutineExceptionImpl(
context,
handlerException(exception, t))
return
}
// If a handler is not present in the context
// or an exception was thrown,
// fallback to the global handler
handleCoroutineExceptionImpl(context, exception)
}
internal actual fun handleCoroutineExceptionImpl(
context: CoroutineContext,
exception: Throwable) {
// use additional extension handlers
for (handler in handlers) {
try {
handler.handleException(context, exception)
} catch (t: Throwable) {
// Use thread's handler
// if custom handler failed to handle exception
val currentThread = Thread.currentThread()
currentThread.uncaughtExceptionHandler
.uncaughtException
(
currentThread,
handlerException(exception, t))
}
}
// use thread's handler
val currentThread = Thread.currentThread()
currentThread.uncaughtExceptionHandler
.uncaughtException
(currentThread, exception)
}

Root Cause

Workaround

fun isNetworkAvailable(): Boolean {
val connectivityManager = getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
return connectivityManager?.activeNetworkInfo?.isConnected == true
}
class ConnectVerifierInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response {
if (!isNetworkAvailable() {
throw IOException("No Network Available!")
}
val request = chain.request()
return chain.proceed(request)
}
}
val okHttpClient = OkHttpClient().newBuilder()
.addInterceptor(ConnectVerifierInterceptor())
.build()
val server = Retrofit.Builder().baseUrl(XXXX).client(okHttpClient).create(APIServer::class.java)

--

--

--

Lead Android & iOS Mobile Engineer at Yahoo (Verizon Media) Taiwan https://www.linkedin.com/in/hanruyeh/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Deal with Quantity Texts in Multi-Language App in Android

Android-10: How to send a customize protected broadcast from system

TimingLogger: An idiomatic Android approach to measuring execution time

Native Android Receive Text from Other Apps in Unity

Flutter Dependency Injection

Capture the objects using Object Detection and Tracking feature by Huawei ML Kit in Android…

Text Recognition and Translation Demo Application Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bram Yeh

Bram Yeh

Lead Android & iOS Mobile Engineer at Yahoo (Verizon Media) Taiwan https://www.linkedin.com/in/hanruyeh/

More from Medium

Integration of Huawei Crash Service in Attendance Tracker Android app (Kotlin) — Part 4

Dagger with Android Achitecture Components using Kotlin

RecycleView Kotlin — Android Studio

Android Library Creation and Usage