Volley is not requesting new Request after manually change the time while app is open

问题: I have an Android app which calls web services using Volley. If my app is being opened and I manually change the time (backward or forward), Volley is not triggering new re...

问题:

I have an Android app which calls web services using Volley. If my app is being opened and I manually change the time (backward or forward), Volley is not triggering new requests and the response which is already in the cache is returning. If I clear cache and data, only then the new request will trigger and new response will appear. How the Volley requests are related to system time and why this is happening? Is there any solution for this?

StringRequest stringRequest = new StringRequest(Request.Method.POST, new ServiceUtils().url,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
           // Log.d(TAG, "onResponse: response " + response);
        }
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       //Log.d(TAG, "onErrorResponse: " + error);
    }
    }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        HashMap<String, String> nameValuePairs = new HashMap<String, String>();
        nameValuePairs.put("parm1", parm1);
        nameValuePairs.put("parm2", parm2);
        nameValuePairs.put("parm3", parm3);
        return nameValuePairs;
    }
};

addToRequestQueue(stringRequest, "", getApplicationContext());

回答1:

Volley caches any request automatically and to overcome this situation, you need to set the setShouldCache attribute to false.

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

Here's a full request copied from the Github issue.

RequestQueue queue = Volley.newRequestQueue(context);
queue.getCache().clear();
StringRequest myReq = new StringRequest(Request.Method.POST,
        VolleyConnector.url,
        createMyReqSuccessListener(),
        createMyReqErrorListener()) {

    protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Cn","1");
        params.put("Yr","1396");
        params.put("MaxInPage","10");
        params.put("Method","Control_Vaziat_View");
        params.put("Pg","1");
        return params;
    };
};
myReq.setShouldCache(false);
queue.add(myReq);

Hope that helps!

  • 发表于 2019-02-15 14:49
  • 阅读 ( 187 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除