Skip to content Skip to sidebar Skip to footer

Got This Error With Retrofit2 & Okhttp3. Unable To Resolve Host "": No Address Associated With Hostname

I am using the retrofit 2 and OkHttp3 to request data from server. I just added a offline cache code but It's not working as expected. I got the error 'Unable to resolve host '<

Solution 1:

I had the same error in my project with kotlin, and I fixed it like this:

client.addInterceptor(provideOfflineCacheInterceptor(context))
client.addNetworkInterceptor(provideCacheInterceptor(context))

privatefunprovideOfflineCacheInterceptor(context: Context): Interceptor {
        return Interceptor { chain ->
            var request = chain.request()
            var cacheHeaderValue = if (!hasNetwork(context)!!){
                    "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 1
                } else {
                    "public, max-age=" + 5
                }
            request = request.newBuilder().header("Cache-Control", cacheHeaderValue).build()
            chain.proceed(request)
        }
    }

    privatefunprovideCacheInterceptor(context: Context): Interceptor {
        return Interceptor { chain ->
            val request = chain.request()
            var cacheHeaderValue = if (!hasNetwork(context)!!){
                    "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 1
                } else {
                    "public, max-age=" + 5
                }
            //request = request.newBuilder().build()val response = chain.proceed(request)
            response.newBuilder()
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    .header("Cache-Control", cacheHeaderValue)
                    .build()
        }
    }

Solution 2:

Your server response has a "Pragma: no-cache" header. You should remove this header in your response interceptor not your request interceptor.

In your current code you've removed it from the request interceptor.

Your provideCacheInterceptor() should look like this:

publicstatic Interceptor provideCacheInterceptor() {
    returnnewInterceptor() {
        @Overridepublic Response intercept(Chain chain)throws IOException {
            Responseresponse= chain.proceed(chain.request());

            // re-write response header to force use of cacheCacheControlcacheControl=newCacheControl.Builder()
                   .maxAge(2, TimeUnit.MINUTES)
                   .build();

           return response.newBuilder()
                    .header(CACHE_CONTROL, cacheControl.toString())
                    .removeHeader("Pragma")
                    .build();
        }
    };
}

Solution 3:

Solution 4:

This happens when trying to access a hostname that is not available on the connected network. Check whether or not you can access this URL through the web browser of the device. If not: There is your problem.

Solution 5:

For me the error was thrown because there was no internet connection in emulator.

Post a Comment for "Got This Error With Retrofit2 & Okhttp3. Unable To Resolve Host "": No Address Associated With Hostname"