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 "
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
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
Oh I see. Here is the link on Stackoverflow
http://stackoverflow.com/questions/38114686/got-this-error-with-retrofit2-okhttp3-unable-to-resolve-host-host-name-n
添加上这些权限就好了,在Androidmanifest 中