0

The server returns a "Last Modified" header based on when the data changes. Need to use this to cache the response in volley.

My Volley request looks something like this:

StringRequest req = new StringRequest(Request.Method.GET, requestUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray result = new JSONArray(response);
                    callback.onSuccess(result);
                } catch (JSONException je) {
                    callback.onSuccess(new JSONArray());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("Error: " + error.getMessage());
            }
        });

Can find examples to cache based on a fixed time - say cache for 5 mins: Android Setup Volley to use from Cache

But this doesnt solve my purpose. The server response has to be invalidated based on the "Last Modified" header. So I'm expecting volley to make the request and get a 304 Not Modified response and hence serve the content from the cache.

Even tried a custom header parser something like this: https://github.com/mcxiaoke/android-volley/blob/master/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java

But this doesnt seem to do the trick either.

Community
  • 1
  • 1
Yashvit
  • 2,337
  • 3
  • 25
  • 32
  • Do you searching for this ?http://stackoverflow.com/questions/21953519/volley-exception-error-when-response-code-304-and-200 – Krish Feb 16 '17 at 16:30
  • 1
    For your purpose `So I'm expecting volley to make the request and get a 304 Not Modified response and hence serve the content from the cache`, I think you should use Googe's Volley library instead of mcxiaoke's library. You can set breakpoint and debug, please read at https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/BasicNetwork.java#103 – BNK Feb 17 '17 at 03:15

0 Answers0