Okhttp: Unable to resolve host "<host-name>": No address associated with hostname.

Created on 28 Jun 2016  ·  3Comments  ·  Source: square/okhttp

Hello,

I am using the retrofit2 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 "": No address associated with hostname." This occurs when It's try to get the retrieve data from the cache(when no internet connection). A code snippet is below.

public static Interceptor provideOfflineCacheInterceptor ()
{
    return new Interceptor()
    {
        @Override
        public Response intercept (Chain chain) throws IOException
        {
            Request request = chain.request();

            if ( !AdeptAndroid.hasNetwork() )
            {
                CacheControl cacheControl = new CacheControl.Builder()
                        .maxStale( 7, TimeUnit.DAYS )
                        .build();

                request = request.newBuilder()
                        .cacheControl( cacheControl )
                        .build();
            }

            return chain.proceed( request );
        }
    };
}

public static Interceptor provideCacheInterceptor ()
{
    return new Interceptor()
    {
        @Override
        public Response intercept (Chain chain) throws IOException
        {
            Response response = chain.proceed( chain.request() );

            // re-write response header to force use of cache
            CacheControl cacheControl = new CacheControl.Builder()
                    .maxAge( 2, TimeUnit.MINUTES )
                    .build();

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

private static Cache provideCache ()
{
    Cache cache = null;
    try
    {
        cache = new Cache( new File( AdeptAndroid.getInstance().getCacheDir(), "http-cache" ),
                           10 * 1024 * 1024 ); // 10 MB
    }
    catch (Exception e)
    {
        Timber.e( e, "Could not create Cache!" );
    }
    return cache;
}

Please have a look at it and help me with this strange issue.

Thanks,
Nikunj

All 3 comments

Thanks for your question. This issue tracker is only for bug reports with test cases and feature requests. Please ask usage questions on Stack Overflow.
https://stackoverflow.com/questions/tagged/okhttp

添加上这些权限就好了,在Androidmanifest 中



Was this page helpful?
0 / 5 - 0 ratings