Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RetrofitCache.getInstance().addIgnoreParam 无效 #22

Open
ningh2018 opened this issue Aug 31, 2018 · 12 comments
Open

RetrofitCache.getInstance().addIgnoreParam 无效 #22

ningh2018 opened this issue Aug 31, 2018 · 12 comments

Comments

@ningh2018
Copy link

你好 ,我使用的是retrofitcachelibrx2 RetrofitCache.getInstance().addIgnoreParam缓存无效, 不加这个忽略没问题。请问是什么问题?

@yale8848
Copy link
Owner

yale8848 commented Sep 1, 2018

你没有用拦截器给请求链接参数特别处理的不需要调用这个函数

@ningh2018
Copy link
Author

就是在每个接口加了时间戳所以才要忽略这个字段,

@ningh2018
Copy link
Author

image

这个地方写法有问题如果忽略字段不是最后一个会报数组越界

@yale8848
Copy link
Owner

yale8848 commented Sep 4, 2018

已经修复 ,请更新至1.0.8

@ningh2018
Copy link
Author

谢谢,数组越界的问题解决了, 但是RetrofitCache.getInstance().addIgnoreParam 这个方法感觉依然无效,我在你的代码上面做了一点点修改增加一个时间戳参数access_token 然后RetrofitCache.getInstance().addIgnoreParam("access_token"); 断网后依然取不到缓存

image

image

image

image

image

@yale8848
Copy link
Owner

yale8848 commented Sep 4, 2018

addIgnoreParam 主要是忽略通过拦截器添加的参数后需要调用的,你看看这个issue: #13

@ningh2018
Copy link
Author

意思我在拦截器里面加上这个时间戳就可以忽略这个参数去取缓存吗?

@yale8848
Copy link
Owner

yale8848 commented Sep 4, 2018

是的,addIgnoreParam本身不需要调用的,对于有些需求比如说是给每个url添加一个参数等方式,这样实际上是修改了原来的链接,这样RetrofitCache是不能正常使用,所以后来我添加了个addIgnoreParam函数

@ningh2018
Copy link
Author

我按照你说的方法吧 access_token 加在拦截器里面,如果access_token 的值是固定的可以取到缓存,如果access_token 的值不是固定的每次都在变动,比如是一个System.currentTimeMillis(), 就取不到缓存了, 是不是有问题?

image

@yale8848
Copy link
Owner

yale8848 commented Sep 7, 2018

你用最新的库吗?我这边测试没有问题

@ningh2018
Copy link
Author

image

是1.0.8,我从git 克隆下拉就加了以下代码其他代码没有变动
image

public OkHttpClient getOkHttpClient(){
OkHttpClient.Builder clientBuilder=new OkHttpClient.Builder();
clientBuilder.readTimeout(20, TimeUnit.SECONDS);
clientBuilder.connectTimeout(20, TimeUnit.SECONDS);
clientBuilder.writeTimeout(20, TimeUnit.SECONDS);
clientBuilder.addInterceptor(new MyInterceptor());
clientBuilder.addInterceptor(new LogInterceptor());
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());

    int cacheSize = 200 * 1024 * 1024;
    File cacheDirectory = new File(mContext.getExternalCacheDir(), "httpcache");
    Cache cache = new Cache(cacheDirectory, cacheSize);
    return clientBuilder.cache(cache).build();
}

class MyInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

// Add access_token parameter if user login
String accessToken = System.currentTimeMillis() + "";
HttpUrl originalUrl = request.url();
if (!TextUtils.isEmpty(accessToken) && originalUrl != null && TextUtils.isEmpty(originalUrl.queryParameter("access_token"))) {
HttpUrl url = originalUrl.newBuilder().addEncodedQueryParameter("access_token", accessToken).build();
request = request.newBuilder().url(url).build();
}
return chain.proceed(request);
}
}

@yale8848
Copy link
Owner

yale8848 commented Sep 8, 2018

好的,那我这边再测测

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants