Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 729
Priyank Patel Avatar asked Sep 30 '15 09:09

Priyank Patel


1 Answers

@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

like image 125
Priyank Patel Avatar answered Nov 14 '22 21:11

Priyank Patel