简体   繁体   English

改造:缓存控制从未包含在响应中

[英]Retrofit: cache-control never included in response

no matter what I do I can never see the header cache-control in the response. 无论我做什么,我都永远不会在响应中看到标头缓存控件

Settings 设置

  • retrofit 1.6.0 改造1.6.0
  • okHttp 1.6.0 okHttp 1.6.0
  • okHttp-urlConnection 1.6.0 okHttp-urlConnection 1.6.0
  • gson 2.2.4 杰森2.2.4
  • Android 4.0+ Android 4.0以上

1. Configuration 1.配置

1.1 RestAdapter with my OkHttpClient 1.1我的OkHttpClient使用RestAdapter

OkHttpClient okHttpClient = new OkHttpClient();
Cache cache = new Cache(getCacheDir(), 1024);
okHttpClient.setCache(cache);

RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(URL)
            .setClient(new OkClient(okHttpClient))
            .setLogLevel(LogLevel.HEADERS)
            .build();

1.2 RestAdapter with the default OkHttpClient Here, when debugging I could see that the client has cache=null; 1.2使用默认OkHttpClient的RestAdapter在这里,在调试时,我可以看到客户端的cache = null;

RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(URL)
            .setLogLevel(LogLevel.HEADERS)
            .build();

2 Log 2日志

For the both configurations the logs are the same. 对于这两种配置,日志都是相同的。

2.1 REQUEST 2.1要求

06-28 13:24:05.601: D/Retrofit(1581): ---> HTTP GET myUri 06-28 13:24:05.601:D / Retrofit(1581):---> HTTP GET myUri

06-28 13:24:05.601: D/Retrofit(1581): Cache-Control: public, max-age=640000 06-28 13:24:05.601:D / Retrofit(1581):缓存控制:公共,最大年龄= 640000

06-28 13:24:05.601: D/Retrofit(1581): ---> END HTTP (no body) 06-28 13:24:05.601:D / Retrofit(1581):---> END HTTP(无内容)

2.2 RESPONSE 2.2回应

06-28 13:24:05.701: D/Retrofit(1581): <--- HTTP 200 myUri (102ms) 06-28 13:24:05.701:D / Retrofit(1581):<--- HTTP 200 myUri(102ms)

06-28 13:24:05.701: D/Retrofit(1581): : HTTP/1.1 200 OK 06-28 13:24:05.701:D / Retrofit(1581)::HTTP / 1.1 200 OK

06-28 13:24:05.701: D/Retrofit(1581): Connection: keep-alive 06-28 13:24:05.701:D / Retrofit(1581):连接:保持活动状态

06-28 13:24:05.701: D/Retrofit(1581): Content-Type: text/html 06-28 13:24:05.701:D / Retrofit(1581):内容类型:text / html

06-28 13:24:05.701: D/Retrofit(1581): Date: Sat, 28 Jun 2014 13:24:01 GMT 06-28 13:24:05.701:D / Retrofit(1581):日期:2014年6月28日星期六13:24:01 GMT

06-28 13:24:05.701: D/Retrofit(1581): OkHttp-Received-Millis: 1403961845708 06-28 13:24:05.701:D / Retrofit(1581):OkHttp收到的Millis:1403961845708

06-28 13:24:05.701: D/Retrofit(1581): OkHttp-Response-Source: NETWORK 200 06-28 13:24:05.701:D / Retrofit(1581):OkHttp-响应-来源:NETWORK 200

06-28 13:24:05.701: D/Retrofit(1581): OkHttp-Sent-Millis: 1403961845680 06-28 13:24:05.701:D / Retrofit(1581):OkHttp-Sent-Millis:1403961845680

06-28 13:24:05.701: D/Retrofit(1581): Server: nginx/1.6.0 06-28 13:24:05.701:D / Retrofit(1581):服务器:nginx / 1.6.0

06-28 13:24:05.701: D/Retrofit(1581): Transfer-Encoding: chunked 06-28 13:24:05.701:D / Retrofit(1581):传输编码:分块

06-28 13:24:05.701: D/Retrofit(1581): Vary: Accept-Encoding 06-28 13:24:05.701:D / Retrofit(1581):不同:接受编码

06-28 13:24:05.701: D/Retrofit(1581): X-Powered-By: PHP/5.5.12-1~dotdeb.1 06-28 13:24:05.701:D / Retrofit(1581):X-Powered-By:PHP / 5.5.12-1〜dotdeb.1

06-28 13:24:05.701: D/Retrofit(1581): <--- END HTTP (-1-byte body) 06-28 13:24:05.701:D / Retrofit(1581):<--- END HTTP(-1字节正文)

EDIT 03/07/2014 编辑2014年3月7日

I noticed that in this question Retrofit+OkHttp is ok when sending GET requests but gives SocketTimetout when sending POST the asker gets "cache-control" in response. 我注意到在此问题中,在发送GET请求时Retrofit + OkHttp可以,但是在发送POST时给出SocketTimetout,请求者获得“缓存控制”作为响应。

04-11 18:00:41.291: D/Retrofit(16390): ---> HTTP GET url 04-11 18:00:41.291:D / Retrofit(16390):---> HTTP GET网址

04-11 18:00:41.291: D/Retrofit(16390): ---> END HTTP (0-byte body) 04-11 18:00:41.291:D / Retrofit(16390):---> END HTTP(0字节正文)

04-11 18:00:42.008: D/Retrofit(16390): <--- HTTP 200 url (716ms) 04-11 18:00:42.008:D / Retrofit(16390):<--- HTTP 200网址(716ms)

04-11 18:00:42.015: D/Retrofit(16390): : HTTP/1.1 200 OK 04-11 18:00:42.015:D / Retrofit(16390)::HTTP / 1.1 200 OK

04-11 18:00:42.015: D/Retrofit(16390): Cache-Control: max-age=0, private, must-revalidate 04-11 18:00:42.015:D / Retrofit(16390):缓存控制:max-age = 0,私有,必须重新验证

Still cannot solve this issue neither in emulators nor in a real device though... 尽管仍然不能在仿真器或实际设备中解决此问题……

我是愚蠢的,我们没有在后端正确设置缓存!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM