Android Volley JsonObjectRequest returns same response every time on mobile data
Asked Answered
S

1

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 ?

Sayers answered 30/9, 2015 at 9:16 Comment(7)
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?Skewness
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.Sayers
Try call request.setShouldCache(false); to check if it worksSkewness
For me it is not working with setShouldCache(false). The new file is not loaded, but the older one on cache.Noctilucent
@Noctilucent have you added setShouldCache(false) before adding request to queue?Sayers
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 successNoctilucent
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);Vantage
S
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

Sayers answered 1/10, 2015 at 5:19 Comment(3)
I think you should set the small softExpire instead, please read my answer hereSkewness
you saved my another day.Gayton
Man you saved my life. Thanks!!Ictus

© 2022 - 2024 — McMap. All rights reserved.