Android Volley JsonObjectRequest returns same response every time on mobile data

烂漫一生 提交于 2020-01-03 12:36:28

问题


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 ?


回答1:


@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



来源:https://stackoverflow.com/questions/32862319/android-volley-jsonobjectrequest-returns-same-response-every-time-on-mobile-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!