Problem: Once an UnknownHostException is returned, the user continues to receive the same error unless the app is reinstalled or the device is rebooted.
Of the users whose OS is Android 11, only a few users are having problems.
The biggest problem is that when an error occurs, the same error is returned continuously for each request.
According to users It is said that re-installing the app or rebooting the device will work again.
It seems that 99% are users of Samsung devices. Sometimes there are also Google Pixel phones.
Both Http and Https give the same error.
Both Wifi, 5G and LTE give the same error.
The request method is using POST and I don't know if it happens to GET as well, I don't use GET.
Also, either Thread is Background or Foreground, or both.
In this my code
Gradle:
android {
minSdkVersion 21
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
/* RETROFIT */
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
/* OKHTTP */
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.9.0'
/* RXJAVA RXANDROID */
implementation 'io.reactivex.rxjava3:rxjava:3.0.11'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
}
Create Request :
interface ApiService {
@POST("get-data")
fun getData(@Body parameter : CustomParameter): Single<Response<CustomObject>>
companion object {
private val rxJava3CallAdapterFactory: RxJava3CallAdapterFactory
get() = RxJava3CallAdapterFactory.createWithScheduler(Schedulers.io())
private fun okHttpClient(): OkHttpClient {
val okHttpClientBuilder = okHttpClientBuilder()
okHttpClientBuilder.addNetworkInterceptor { chain ->
val request = chain.request()
val response = chain.proceed(request)
if (response.code >= 400) {
handleNetworkError()
}
response
}
okHttpClientBuilder.addInterceptor { chain ->
val request = chain.request()
chain.proceed(request)
}
return okHttpClientBuilder.build()
}
fun createApiService(context: Context): ApiService {
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.client(okHttpClient())
.build()
return retrofit.create(ApiService::class.java)
}
}
}
Call Request (In Activity):
ApiService.createMainWeatherApiService().getData(CustomParameter())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ res ->
handleSuccess()
},
{ error ->
// UnknownHostException!!!!
handleFail()
}
).apply { compositeDisposable.add(this) }
I created an issue on okhttp : https://github.com/square/okhttp/issues/6591