CodexBloom - Programming Q&A Platform

best practices for 'No route to host' scenarios when using Retrofit with OkHttp on Android 14?

๐Ÿ‘€ Views: 58 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-08-22
android retrofit okhttp networking error-handling kotlin

I'm following best practices but Quick question that's been bugging me - I'm currently developing an app that fetches data from a remote API using Retrofit and OkHttp. Recently, I've been working with a 'No route to host' behavior when trying to make network requests. This scenario arises intermittently, particularly when the device is connected to certain Wi-Fi networks. The behavior message in the logcat reads: ``` E/OkHttp: call failed: java.net.NoRouteToHostException: No route to host ``` I've tried switching between different networks and even testing on mobile data, but the question continues in specific conditions. My current Retrofit setup looks like this: ```kotlin val retrofit = Retrofit.Builder() .baseUrl("https://api.example.com/") .addConverterFactory(GsonConverterFactory.create()) .client(OkHttpClient.Builder().build()) .build() ``` I confirmed that the URL is correct and reachable using Postman, and Iโ€™ve also checked the device's network permissions in the Android Manifest: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` Additionally, Iโ€™ve implemented connection timeouts in OkHttp: ```kotlin val client = OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build() ``` Iโ€™m not using any proxy settings. After some debugging, I suspect that it might be related to the DNS resolution when on some networks. To further investigate, Iโ€™ve tried using a different DNS provider (like Googleโ€™s 8.8.8.8) by modifying the DNS settings, but the scenario still occurs. Has anyone faced a similar scenario or can suggest steps to diagnose this further? Any guidance would be greatly appreciated! Has anyone else encountered this? I'd be grateful for any help.