Stepan Sanda
Stepan Sanda

Reputation: 2340

Volley Cache using ETag

I'm using Volley library and I can't to set up caching properly.

Server sends me json object, expiration and ETAg.. I want to Voley save this object in cache and in next request for this object use request to the server including ETag in the header. If response will be 304 Not Modified, then it should use cached resource and if it will be 200 OK, it should use new resource from the server.

I'm watching communication via Fidler and I can see that Volley doesn't send request at all (if the cache isn't expired) or if it is expired it sends new request with If-None-Match + etag string.. and server always response with 200ok

Upvotes: 3

Views: 3279

Answers (2)

Shawn Thye
Shawn Thye

Reputation: 936

I realise that when i use

BasicHttpClient httpClient = new BasicHttpClient();

httpClient.addHeader("If-None-Match", "vENFxg");

It always return 200.

but if I use

httpClient.addHeader("If-None-Match", "\"vENFxg\"");

it works, and I get 304(HTTP_NOT_MODIFIED).

You can try below with volley.

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    mHeaders = new HashMap<String, String>();
    mHeaders.put("If-None-Match", "\"vENFxg\"");

    return mHeaders;
}

let me know if worked.

I assume that you store the volley response if somewhere in your app because you needed etag.

In this case, I would prefer to save the etag in somewhere(like Preferences). I do use volley with some of simple request.

Volley's presentation also mention "volley perfectly fine for doing those same kinds of operations in the background. But it really excels at the intersection of UI and network. It's not for everything, though."

And sorry, my English is poor.

Upvotes: 0

Jokin
Jokin

Reputation: 4218

If you want to revalidate on each request, you should set the max age to 0 so the cache entry is always expired.

And if the server always response with 200 OK, check the cause in the server, because if the request has the correct Etag has nothing to do with volley.

Upvotes: 0

Related Questions