2013-09-26 58 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);