Reputation: 57
I am getting a Fatal exception while making an API call using co-routine and retrofit in android kotlin. And app crashes with below exception.
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-7
Process: com.jamhub.barbeque, PID: 18525
java.net.SocketTimeoutException: timeout
at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:656)
at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:664)
at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.java:153)
at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:131)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
Tried adding time out, CoroutineExceptionHandler
val client = OkHttpClient().newBuilder().addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder()
.build()
return chain.proceed(request)
}
}).callTimeout(30, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
retrofitSocial = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
It shouldn't crash if the exception has occurred ideally it should return the request failure.
Upvotes: 2
Views: 6547
Reputation: 2263
As per the error crashing the app - in your call logic you're try-catching the wrong thing :) It should be formed like this:
try {
val response = api?.generateOTP(otpRequestBody)
withContext(Dispatchers.Main) {
when (response?.code()) { } }
catch (e: IOException) {
} /* there are many exceptions thrown by Retrofit, all are subclasses of IOException */
Since it's not response?.code()
that's throwing exception, but the api?.generateOTP(otpRequestBody)
.
As for the timeout itself - you might have wrong URL, weak internet connection, you'll need to provide more info for us to find the reason out :)
Or you can try the CoroutineExceptionHandler
:
val exceptionHandler = CoroutineExceptionHandler{_ , throwable->
throwable.printStackTrace()
}
//when you make request:
scope.launch(Dispatchers.IO + exceptionHandler ){
}
Upvotes: 6