【问题标题】:Volley is not requesting new Request after manually change the time while app is open在应用程序打开时手动更改时间后,Volley 未请求新请求
【发布时间】:2019-02-14 13:42:18
【问题描述】:

我有一个使用 Volley 调用网络服务的 Android 应用。如果我的应用程序正在打开并且我手动更改时间(向后或向前),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());

【问题讨论】:

    标签: java android android-volley


    【解决方案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);
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      相关资源
      最近更新 更多