0

Im working on with volley, i have issue in retrieving the cache. Every time I'll to access the cache for display It is empty. But when i check it on the sdcard I can see a files under cache folder in my application.

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


}

I can't figure out what i am missing.

thanks

chkm8
  • 1,302
  • 1
  • 13
  • 34

1 Answers1

0

After, testing for a couple of times, i found out that when you use volley and when you want to have a cache, you need to use a GET request not POST.

But my question is, is there a way that we can still use cache when using post?

chkm8
  • 1,302
  • 1
  • 13
  • 34
  • According to RFC 2616 Section 9.5: "Responses to POST method are not cacheable, UNLESS the response includes appropriate Cache-Control or Expires header fields." http://stackoverflow.com/a/828080/1022454 – Jonathon Fry Oct 02 '14 at 09:12