Android 截击缓存为空
我正在处理截击,我在检索缓存时遇到了问题。每次我要访问缓存进行显示时,它都是空的。但当我在SD卡上检查它时,我可以在我的应用程序中看到缓存文件夹下的文件Android 截击缓存为空,android,android-volley,Android,Android Volley,我正在处理截击,我在检索缓存时遇到了问题。每次我要访问缓存进行显示时,它都是空的。但当我在SD卡上检查它时,我可以在我的应用程序中看到缓存文件夹下的文件 // We first check for cached request Cache cache = AppController.getInstance().getRequestQueue().getCache(); Cache.Entry entry = cache.get(URL_DASHBOARD); AppC
// 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);
}
/**执行截击*/
私有void executeVolleyRequest(字符串路径){
//发出新的截击请求并获取json数组
String tag_String_req=“String_req”;
StringRequest StringRequest=新的StringRequest(Request.Method.POST,path,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.e(标记“loadVolleyConnection:从api获取数据:”+响应);
试一试{
JSONObject temp_obj=新JSONObject(响应);
JSONArray temp_array=temp_obj.getJSONArray(JSON_DASHBOARD.array_DASHBOARD.getVal().toString());
setAdapter_仪表板(临时阵列);
如果(已刷新){
//刷新列表后,调用onRefreshComplete。
(myListView).onRefreshComplete();
isRefreshed=假;
设置日期(临时数组);
Log.e(标签“loadVolleyConnection:isRefreshed..”;
}否则{
setAdapter_仪表板(临时阵列);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
Log.e(标记,“Error:+volleyError.getClass().getSimpleName());
if(volleyError.networkResponse==null){
if(volleyError.getClass().equals(TimeoutError.class)){
//显示超时错误消息
Alertmessage(“登录错误”,“Oops.Timeout错误!请检查您的连接”);
}
}否则{
Alertmessage(“登录错误”,“检查连接。/n”);
}
//pDialog.disclose();
}
}){
};
setRetryPolicy(新的DefaultRetryPolicy(5000,2,1));
stringRequest.setShouldCache(true);
//将请求添加到请求队列
AppController.getInstance().addToRequestQueue(stringRequest,标记字符串请求);
}
我不知道我错过了什么
谢谢你,经过几次测试,我发现当你使用截击和想要缓存时,你需要使用GET请求而不是POST
但我的问题是,在使用post时,有没有一种方法仍然可以使用缓存?根据RFC 2616第9.5节:“对post方法的响应是不可缓存的,除非响应包含适当的缓存控制或Expires标头字段。”