有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个安卓应用程序,可以通过截击调用web服务。如果我的应用被打开,我手动更改时间(向后或向前),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());

共 (1) 个答案

  1. # 1 楼答案

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

    希望有帮助