Retrofit v2.4.0没有发送If-Modified-Since标头 [英] Retrofit v2.4.0 is not sending the If-Modified-Since header

查看:99
本文介绍了Retrofit v2.4.0没有发送If-Modified-Since标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常基本的问题,但是我已经没有足够的想法了.
Retrofit v2.4.0没有发送 If-Modified-Since 标头,因此缓存无法正常工作.

This may be a very basic question, but I've ran out of ideas.
Retrofit v2.4.0 is not sending the If-Modified-Since header, as a result caching is not working.

我每天要轮询服务器几次,以查看是否有任何更新的数据,因此需要 If-Modified-Since 标头. (推送通知可能会在新版本中实现)

I'm polling the server several times a day to see if is there any updated data, hence the need for If-Modified-Since header. (push notifications may be implemented in a new release)

根据本文,设置非常简单:

Based on this article, the setup is extremely easy: https://futurestud.io/tutorials/retrofit-2-activate-response-caching-etag-last-modified
I've read several related articles, but those were focused on the use-cases when the server's implementation was either inaccessible or it didn't send the headers. This is not my case. Those suggested the usage of networkInterceptors(). As the correct response headers are sent, I shouldn't need an interceptor (I guess).

从理论上讲,它应该起作用.

Theoretically it should work.

基于响应头,看起来服务器已正确配置.

Based on the response headers, it looks like that the server is correctly configured.

代码如下:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

Cache cache = new Cache(getApplication().getCacheDir(), 30 * 1024 * 1024);

httpClient = new OkHttpClient.Builder()
    .cache(cache)
    .addInterceptor(logging)
    .build();

retrofit = new Retrofit.Builder()
    .baseUrl("http://someserver:8080/")
    .callbackExecutor(Executors.newSingleThreadExecutor())
    .client(httpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

日志:

D/OkHttp:-> GET http://someserver:8080/model/modelId http/1.1
D/OkHttp:-> END GET

D/OkHttp: --> GET http://someserver:8080/model/modelId http/1.1
D/OkHttp: --> END GET

<-200 OK http://someserver:8080/model/modelId (23毫秒)
D/OkHttp:缓存控制:私有
D/OkHttp:内容长度: 3240854
D/OkHttp:内容类型:application/octet-stream
D/OkHttp:最后修改时间:2018年5月14日,星期一,格林尼治标准时间
D/OkHttp:日期:2018年5月14日星期一09:03:50 GMT
D/OkHttp:<-结束 HTTP

<-- 200 OK http://someserver:8080/model/modelId (23ms)
D/OkHttp: Cache-Control: private
D/OkHttp: Content-Length: 3240854
D/OkHttp: Content-Type: application/octet-stream
D/OkHttp: Last-Modified: Mon, 14 May 2018 07:22:25 GMT
D/OkHttp: Date: Mon, 14 May 2018 09:03:50 GMT
D/OkHttp: <-- END HTTP

请让我知道我在做什么错

Please let me know what am I doing wrong.

推荐答案

您的服务器的缓存配置不正确.如果您查看本文的故障排除部分,您会发现它必须为Cache-Control: private, must-revalidate.

Your server's cache configuration is incorrect. If you look at this article's Troubleshooting section you'll notice that it needs to be Cache-Control: private, must-revalidate.

这篇关于Retrofit v2.4.0没有发送If-Modified-Since标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆