Search code examples
androidjsonandroid-volleyjsonobjectrequest

Android Volley JsonObjectRequest returns same response every time on mobile data


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 ?


Solution

  • @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