2013-09-26 61 views
2

我测试凌空有凌空缓存行为问题..Android的凌空缓存

我的代码:

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

我从服务器得到这样的响应:{“一”:” 111" , “b”: “222”}

到目前为止一切都确定..

但是当我改变服务器如数据:{ “一个”: “111”, “B”: “333”},然后重新开始该应用程序,凌空得到像之前相同的响应.. { “一个”: “111”, “B”: “222”} 。

我觉得voley缓存旧的请求..我怎样才能改变呢?我想每一个从服务器实际数据的时间..

编辑:
我解决了这个“愚蠢”的问题..
只需添加:标题(“缓存控制:无缓存”); 在PHP文件..

+2

您可以编写和标记,作为一个答案,可能对他人有所帮助。 :) –

回答

2

befor队列中添加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);