HTTP 304, cache and headers

385 views
Skip to first unread message

Simon Guerout

unread,
Aug 19, 2013, 10:03:21 AM8/19/13
to volley...@googlegroups.com
Hi all,

I faced an issue while handling the responses' encoding in my requests :
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

The first time I loaded data from the server, the encoding was OK, but if I restarted my activity, something was wrong with the encoding.
I ended up dumping the headers, and some were missing :
protected Response<T> parseNetworkResponse(NetworkResponse response) { 
[...] 
for (String key : response.headers.keySet()) {
Log.w(TAG, key + " = " + response.headers.get(key));
[...] 
}

I had two different sets of headers each time. The second time, the content-type header was missing.
I dumped the cache entry's headers, and I found that they were good the second time, then wrong the third time.
And I noticed that the response.notModified field was set to true.

So, what happened here ?
=> On a 304 response, all the headers are not sent a second time, and I was not able to parse them, and that's why my encoding ended up wrong!

I ended up merging the new headers over the old headers (not sure if keeping only the old ones is completely OK).
Here's the solution to this issue in my request class :
protected Response<T> parseNetworkResponse(NetworkResponse response) { 
[...] 
if (response.notModified && getCacheEntry() != null && getCacheEntry().responseHeaders != null) {
for (String key : response.headers.keySet()) {
getCacheEntry().responseHeaders.put(key, response.headers.get(key));
}
for (String key : getCacheEntry().responseHeaders.keySet()) {
response.headers.put(key, getCacheEntry().responseHeaders.get(key));
}
}
[...] 
}

It would be nice to have this kind of stuff done directly in the library, wouldn't it ?

Simon Guerout

unread,
Aug 19, 2013, 10:06:48 AM8/19/13
to volley...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages