8

I am using Volley JsonObjectRequest to get data from server.

code snippet:

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        System.out.println("Response: " + response.toString());
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});

But I am getting same JSONObject response every time on mobile data connection.

Note: It's work perfectly on WiFi connection.

Is anyone facing this issue ? any solution ?

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • Can you clarify that? Get same response is not the expected one? Can you post the server Url or server code for checking? Or you mean the response cached? – BNK Sep 30 '15 at 09:25
  • Yes I have checked that url with other HTTP Requester client and that is not server issue. Volley returns old response every time in mobile data only. – Priyank Patel Sep 30 '15 at 09:28
  • 1
    Try call request.setShouldCache(false); to check if it works – BNK Sep 30 '15 at 09:30
  • For me it is not working with setShouldCache(false). The new file is not loaded, but the older one on cache. – TonyL Aug 20 '19 at 12:31
  • @TonyL have you added `setShouldCache(false)` before adding request to queue? – Priyank Patel Aug 20 '19 at 12:37
  • Yes I did. From the PC browser I can see the updated file, but the android emulator always return the old values. I cleared the app cache and reinstalled with no success – TonyL Aug 20 '19 at 12:51
  • I tried changing the date by increasing it and then i corrected the date again but the volley seems to get the last request which will always depend the date so i guess you should always use request.setShouldCache(false); – Farido mastr Feb 18 '21 at 08:29

1 Answers1

22

@BNK request.setShouldCache(false); worked for me. It's issue of volley cache management.

I assume that, when a request is sent:

  • It would hit the cache first and send that to onResponse

  • then when the results come through from the remote server it would provide it to the onResponse

If you use any of the default Request classes implemented in volley(e.g. StringRequest, JsonRequest, etc.), then call setShouldCache(false) right before adding the request object to the volley RequestQueue

request.setShouldCache(false);
myQueue.add(request);

You can also set expiration policy for cache.

See this answer for more details

Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85