When receiving a 304, volley is using the 304 response headers, instead of the cache ones.

1,201 views
Skip to first unread message

Miguel Cohnen

unread,
Jul 18, 2013, 3:32:45 PM7/18/13
to volley...@googlegroups.com
If I do a request that is cached, and I get a 304, Volley is ignoring the cache headers and just using the 304 response ones. 

Problem with this is that a header like "Content-Type" will disappear, even though it was in the original request.

I added this fix: 

iff --git a/src/com/android/volley/toolbox/BasicNetwork.java b/src/com/android/volley/toolbox/BasicNetwork.java
index b3c7d45..1c76919 100644
--- a/src/com/android/volley/toolbox/BasicNetwork.java
+++ b/src/com/android/volley/toolbox/BasicNetwork.java
@@ -86,6 +86,9 @@ public class BasicNetwork implements Network {
             HttpResponse httpResponse = null;
             byte[] responseContents = null;
             Map<String, String> responseHeaders = new HashMap<String, String>();
+            if (request.getCacheEntry() != null) {
+                responseHeaders.putAll(request.getCacheEntry().responseHeaders);
+            }
             try {
                 // Gather headers.
                 Map<String, String> headers = new HashMap<String, String>();
@@ -94,7 +97,8 @@ public class BasicNetwork implements Network {
                 StatusLine statusLine = httpResponse.getStatusLine();
                 int statusCode = statusLine.getStatusCode();

-                responseHeaders = convertHeaders(httpResponse.getAllHeaders());
+                responseHeaders.putAll(convertHeaders(httpResponse.getAllHeaders()));
+
                 // Handle cache validation.
                 if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                     return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,

Let me know if this is correct.

Thank you!

Simon Guerout

unread,
Aug 19, 2013, 10:05:57 AM8/19/13
to volley...@googlegroups.com
Just read your topic, I faced the same issue but used another solution : https://groups.google.com/d/msg/volley-users/Qmp7vBPbFOY/XAd5Uvs22JMJ
Yours looks much better !

Miguel Cohnen

unread,
Aug 19, 2013, 10:09:17 AM8/19/13
to volley...@googlegroups.com
Awesome, glad to see I was not alone on this one :)

Vinayraj Singh Solanki

unread,
Sep 25, 2014, 5:57:10 AM9/25/14
to volley...@googlegroups.com
Thanks for the solution, this helped me a lot and saved a lot of time.

Ralph Bergmann | the4thFloor.eu

unread,
Oct 9, 2014, 12:05:11 PM10/9/14
to volley...@googlegroups.com
A patch was merge to fix it, see
https://android-review.googlesource.com/#/c/110241/


Ralph

signature.asc
Reply all
Reply to author
Forward
0 new messages