I am working on android native kotlin project.
I am testing app in android real Oneplus 6 device with Android version 10.
Please find below details from build.gradle file for latest okhttp and retrofit dependencies which I am using for project.
okhttp
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"`
Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
I have already set relevant timeouts for okhttpclient.
.connectTimeout(HTTP_CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(HTTP_READ_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HTTP_WRITE_TIMEOUT, TimeUnit.SECONDS)
I am using okhttp3.Interceptor. Please find implementation below.
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
class AuthorizationInterceptor(private val sharedPreferenceService: SharedPreferenceService) :
Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
return chain.proceed(requestBuilder.build())
}
}
Observations / Issues
- If API call is in progress and we try to change or disable network/internet, okhttp3 itself internally crashing as soon as below line executes from above snippets so App is crashing.
return chain.proceed(requestBuilder.build())
I have figured out below okhttp internal crashes logs from my android studio that can help you to find root cause and fixes.
FATAL EXCEPTION: main
Process: com.pg.posm, PID: 8770
javax.net.ssl.SSLException: Read error: ssl=0x7afca87588: I/O error during system call, Software caused connection abort
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pg.posm, PID: 26238
java.net.ConnectException: Failed to connect to hostname/ip:port
I also tried to wrap above Interceptor snippets using try-catch but still okhttp3 itself crashing so App is crashing.
We don't have any control to solve it.
Please find below Interceptor snippets with wrapped try-catch implementation.
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import timber.log.Timber
import java.io.IOException
class AuthorizationInterceptor(private val sharedPreferenceService: SharedPreferenceService) :
Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
try {
return chain.proceed(requestBuilder.build())
}catch (e: Throwable) {
if (e is IOException) {
throw e
} else {
throw IOException(e)
}
}
}
}
Anyone can try to make small android project with relevant setup and snippets and see.
Please provide necessary fixes or approach for this blocker issue as many people are facing in the world.
Please let me know in case you need more details. I would more than happy to provide you.