java.net.UnknownHostException, Caused by: android.system.GaiException:
TieuNhi95 opened this issue · comments
Coroutine: 1.3.9
retrofit: 2.9.0
gson: 2.9.0
logging-interceptor: 4.9.3
Crash log on play console, the amount of crashes is a lot, please tell me how to solve it!
java.net.UnknownHostException: at java.net.Inet6AddressImpl.lookupHostByName (Inet6AddressImpl.java:156) at java.net.Inet6AddressImpl.lookupAllHostAddr (Inet6AddressImpl.java:103) at java.net.InetAddress.getAllByName (InetAddress.java:1152) at okhttp3.Dns$Companion$DnsSystem.lookup (Dns.kt:49) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress (RouteSelector.kt:164) at okhttp3.internal.connection.RouteSelector.nextProxy (RouteSelector.kt:129) at okhttp3.internal.connection.RouteSelector.next (RouteSelector.kt:71) at okhttp3.internal.connection.ExchangeFinder.findConnection (ExchangeFinder.kt:205) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection (ExchangeFinder.kt:106) at okhttp3.internal.connection.ExchangeFinder.find (ExchangeFinder.kt:74) at okhttp3.internal.connection.RealCall.initExchange$okhttp (RealCall.kt:255) at okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:76) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:201) at okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:517) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641) at java.lang.Thread.run (Thread.java:919) Caused by: android.system.GaiException: at libcore.io.Linux.android_getaddrinfo (Native Method) at libcore.io.ForwardingOs.android_getaddrinfo (ForwardingOs.java:74) at libcore.io.BlockGuardOs.android_getaddrinfo (BlockGuardOs.java:200) at libcore.io.ForwardingOs.android_getaddrinfo (ForwardingOs.java:74) at java.net.Inet6AddressImpl.lookupHostByName (Inet6AddressImpl.java:135) at java.net.Inet6AddressImpl.lookupAllHostAddr (Inet6AddressImpl.java:103) at java.net.InetAddress.getAllByName (InetAddress.java:1152) at okhttp3.Dns$Companion$DnsSystem.lookup (Dns.kt:49) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress (RouteSelector.kt:164) at okhttp3.internal.connection.RouteSelector.nextProxy (RouteSelector.kt:129) at okhttp3.internal.connection.RouteSelector.next (RouteSelector.kt:71) at okhttp3.internal.connection.ExchangeFinder.findConnection (ExchangeFinder.kt:205) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection (ExchangeFinder.kt:106) at okhttp3.internal.connection.ExchangeFinder.find (ExchangeFinder.kt:74) at okhttp3.internal.connection.RealCall.initExchange$okhttp (RealCall.kt:255) at okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:76) at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:201) at okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt:517) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641) at java.lang.Thread.run (Thread.java:919)
Looks like the device is offline and you should be catching the exception and do some error handling. Not a bug in OkHttp.
@connyduck
i think UnknownHostException is IOException and retrofit auto catch this exception, why it still crash?
I tested, the app works fine when offline (no crash), Can you give me a suggestion?
No Retrofit does not auto catch it. Depends on the adapter you are using. Can you show me your code?
My apiModule:
`@Module
@Installin(SingletonComponent::class)
class ApiModule {
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().setLenient().create()
}
@Provides
@Singleton
fun provideApiInterface(gson: Gson, client: OkHttpClient)
: ApiInterface {
val retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.API_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return retrofit.create(ApiInterface::class.java)
}
@Provides
@Singleton
fun provideHttpClient(cache: Cache?): OkHttpClient {
return OkHttpClient.Builder()
.cache(cache)
.connectTimeout(Constants.DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
.readTimeout(Constants.DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
.writeTimeout(Constants.DEFAULT_TIMEOUT.toLong(), TimeUnit.SECONDS)
.build()
}
@Provides
@Singleton
fun provideCache(@ApplicationContext application: Context): Cache {
val cacheSize = 10 * 1024 * 1024.toLong() // 10 MB
val httpCacheDirectory = File(application.cacheDir, "http-cache")
return Cache(httpCacheDirectory, cacheSize)
}
}`
You should catch and handle network errors, this is not a bug that we can help with.
@yschimke @Connyduct
I have caught the exception by this way.
` private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
Timber.tag(TAG).e("error: ${throwable.message}")
handleError(throwable)
}
val coroutineContextIO = Dispatchers.IO + exceptionHandler
private fun getDomain() {
viewModelScope.launch(coroutineContextIO) {
val jsonString = ObjectDevice(
client = Constants.CLIENT,
platform = Constants.PLATFORM_ANDROID,
appVersion = BuildConfig.VERSION_CODE,
device = Build.DEVICE
)
val asyncTwo = async {
apiInterface.getDomain(jsonString.toRequestBodyEncrypted(gson))
}
val response = asyncTwo.await()
val data = gson.fromJson(
response.string().decrypt(),
ResponseGetDomain::class.java
)
saveDomainInfo(data)
checkLogin()
}
}`
A network error is normal, not something you would typically log as an error to logcat. Why not try catch in the code?