0
即時通訊使用排球,我有檢索緩存問題。每次我要訪問緩存以供顯示時它都是空的。但是當我在SD卡上檢查它時,我可以在我的應用程序中看到緩存文件夾下的文件。排空緩存爲空
// We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(URL_DASHBOARD);
AppController.getInstance().getRequestQueue().getCache().invalidate(URL_DASHBOARD, true);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
JSONObject temp = new JSONObject(data);
Log.e(TAG,"loadVolleyConnection: fetch data from cache: "+temp.toString());
setAdapter_dashboard(temp.getJSONArray(JSON_DASHBOARD.ARRAY_DASHBOARD.getVal().toString()));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
Log.e(TAG,"loadVolleyConnection: query to api 「);
executeVolleyRequest(URL_DASHBOARD);
}
executeVolleyRequest
/**Execute Volley*/
private void executeVolleyRequest(String path){
//Making fresh volley request and getting json array
String tag_string_req = "string_req";
StringRequest stringRequest = new StringRequest(Request.Method.POST,path,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG,"loadVolleyConnection: fetch data from api: "+response);
try {
JSONObject temp_obj = new JSONObject(response);
JSONArray temp_array = temp_obj.getJSONArray(JSON_DASHBOARD.ARRAY_DASHBOARD.getVal().toString());
setAdapter_dashboard(temp_array);
if(isRefreshed){
// Call onRefreshComplete when the list has been refreshed.
(myListView).onRefreshComplete();
isRefreshed = false;
setUpdate(temp_array);
Log.e(TAG,"loadVolleyConnection: isRefreshed..");
}else{
setAdapter_dashboard(temp_array);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "Error: " + volleyError.getClass().getSimpleName());
if (volleyError.networkResponse == null) {
if (volleyError.getClass().equals(TimeoutError.class)) {
// Show timeout error message
Alertmessage("Login Error","Oops.Timeout error!Please check your connection.");
}
}else{
Alertmessage("Login Error","Check your Connection./n");
}
//pDialog.dismiss();
}
}){
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,2,1));
stringRequest.setShouldCache(true);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}
我想不出我缺少什麼。
感謝
根據RFC 2616第9.5節: 「對POST方法的響應不可緩存,除非響應包含適當的Cache-Control或Expires標頭字段。」 http://stackoverflow.com/a/828080/1022454 – 2014-10-02 09:12:42