2

我正在测试 Volley 并对凌空缓存行为有疑问..

我的代码:

RequestQueue queue = Volley.newRequestQueue(this); 
    final String url = "http://www.mywebsite.com/test.php";

    // prepare the Request
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() 
        {
            @Override
            public void onResponse(JSONObject response) {   
                            // display response     
                Log.d("Response", response.toString());
            }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {            
                Log.d("Error.Response", "test");
           }
        }
    );

    // add it to the RequestQueue   
    queue.add(getRequest);

我从服务器收到此响应: {"a":"111","b":"222"}

到目前为止一切正常。。

但是当我更改服务器上的数据时,例如: {"a":"111","b":"333"} 并再次启动应用程序时,凌空得到与以前相同的响应.. {"a":"111 ","b":"222"}。

我认为 voley 缓存了旧请求.. 我该如何更改?我想要每次来自服务器的实际数据..

编辑:
我解决了“愚蠢”问题..
只需添加: header("Cache-Control: no-cache"); 在php文件中..

4

1 回答 1

2

在队列中添加 getReuest 之前,只需添加一行

queue.getCache().clear();

通过使用它,您可以清除凌空缓存的缓存,并且每次您获得来自服务器的响应时。你可以这样做

 RequestQueue queue = Volley.newRequestQueue(this); 
  final String url = "http://www.mywebsite.com/test.php";

// prepare the Request 
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener<JSONObject>() 
    { 
        @Override 
        public void onResponse(JSONObject response) {   
                        // display response      
            Log.d("Response", response.toString());
        } 
    },  
    new Response.ErrorListener() 
    { 
         @Override 
         public void onErrorResponse(VolleyError error) {            
            Log.d("Error.Response", "test");
       } 
    } 
); 
//to clear the cache 
queue.getCache().clear();

// add it to the RequestQueue    
queue.add(getRequest);
于 2015-10-23T10:05:38.200 回答