在应用程序打开时手动更改时间后,Volley 未请求新请求

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

我有一个 Android 应用程序,它使用 Volley 调用网络服务。如果我的应用程序正在打开并且我手动更改时间(向后或向前),Volley 不会触发新请求并且已经在缓存中的响应正在返回。如果我清除缓存和数据,只有这样新的请求才会触发,新的响应才会出现。 Volley 请求如何与系统时间相关以及为什么会发生这种情况?有解决办法吗?

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());

Volley 会自动缓存任何请求,要克服这种情况,您需要将 setShouldCache 属性设置为 false

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

这是从 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);

希望对您有所帮助!