2

Ways to implement volley json response cache.I tried the following way to get response from volley.i get the response correctly.I dont know how to store these json values into volley cache

StringRequest strReq = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    System.out.println("mainresp$$$"+response);
                    Log.d("Volley Request Success", response.toString());
                    result=response;
                    callback.onSuccess(result);


                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("volley request Error",
                    "Error: " + error.getMessage());

        }
    }) {

        @Override
        protected Map<String, String> getParams() {


            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
BNK
  • 23,994
  • 8
  • 77
  • 87
Booshan
  • 97
  • 1
  • 3
  • 11
  • 2
    http://stackoverflow.com/questions/16781244/android-volley-jsonobjectrequest-caching/16852314#16852314 – Booshan Sep 30 '15 at 08:34
  • i dont know how to implement this functionality..that provided in the above links...Please give some ideas to proceed for response caching using volley – Booshan Sep 30 '15 at 08:35
  • Please take a look at [my answer here](http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946) – BNK Sep 30 '15 at 09:28
  • Thanks for your answer ..i will try this – Booshan Sep 30 '15 at 09:48
  • But why do you want cache for a POST request? Cache for GET is reasonable :-) – BNK Sep 30 '15 at 10:02
  • i want to cache the response values for a POST request..because i have to work on api call..when the internet availabe it fetch values from server...........when internet not available i have to fetch the values from cache (i.e JSON values)..this is my requirement – Booshan Sep 30 '15 at 10:32
  • i have try this..this is not work for POST REQUEST why? there is no option for GET and POST in your method? – Booshan Sep 30 '15 at 10:34
  • I think Cache only for GET, you can google search more. In my sample, 0 value is GET (1 is POST) – BNK Sep 30 '15 at 10:36
  • However, I think you can try my code with your POST to check if it works, I will check myself when having free time :-) – BNK Sep 30 '15 at 10:42
  • i try your code..it not works for me..post request...is there any option to indicate whether the request is POST or GET in code? – Booshan Sep 30 '15 at 10:46
  • The first param is often the type of request. As in your StringRequest, Request.Method.POST is 1 – BNK Sep 30 '15 at 11:15

1 Answers1

3

Together with my comments, you have already read my answer at the following question:

Android Setup Volley to use from Cache

I have just tested with POST request, as the following code:

        CacheRequest cacheRequest = new CacheRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
            @Override
            public void onResponse(NetworkResponse response) {
                try {
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    // Check if it is JSONObject or JSONArray
                    Object json = new JSONTokener(jsonString).nextValue();
                    JSONObject jsonObject = new JSONObject();
                    if (json instanceof JSONObject) {
                        jsonObject = (JSONObject) json;
                    } else if (json instanceof JSONArray) {
                        jsonObject.put("success", json);
                    } else {
                        String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}";
                        jsonObject = new JSONObject(message);
                    }
                    textView.setText(jsonObject.toString(5));                        
                } catch (UnsupportedEncodingException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // do something...
            }
        });

My sample Asp.Net Web API code as the following:

        // POST api/<controller>
        public IHttpActionResult Post()
        {
            string jsonString = "[" +
                                     "{" +
                                         "name: \"Person 1\"," +
                                         "age: 30," +
                                         "type: \"POST\"," +
                                     "}," +
                                     "{" +
                                         "name: \"Person 2\"," +
                                         "age: 20," +
                                         "type: \"POST\"," +
                                     "}," +
                                     "{" +
                                         "name: \"Person 3\"," +
                                         "age: 40," +
                                         "type: \"POST\"," +
                                     "}" +
                                "]";            
            JArray jsonObj = JArray.Parse(jsonString);
            return Ok(jsonObj);
        }

Here is the result screenshot

result screenshot

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • Glad it could help you! – BNK Oct 01 '15 at 05:54
  • 1)Network not available... 1) check the response for the corresponding url in cache ...2) reterive the response from cache – Booshan Oct 01 '15 at 05:58
  • 1) when connected to internet .....1) api call starts fresh.....2) store response in cache – Booshan Oct 01 '15 at 06:00
  • IMO, you can read more [here](http://stackoverflow.com/questions/32045077/why-volley-doesnt-clear-then-update-the-cache-when-getting-new-data) – BNK Oct 01 '15 at 06:03
  • `final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely` – BNK Oct 01 '15 at 06:04
  • is it possible to clear cache for new api call for that particular api values stored in cache? – Booshan Oct 01 '15 at 07:30
  • I have not tried yet, however, you can read [here](http://stackoverflow.com/questions/24464610/how-to-clear-the-volley-cache-automatically) and [here](http://stackoverflow.com/questions/25287161/volley-networkimageview-clear-cached-image) then try. – BNK Oct 01 '15 at 07:38