0

This is my JSON code. I want to cache JSON data from URL for the first time and second time it will be work without internet.How and where have to use the volley cache tool, and how to access the saved file??

StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.URL_CONTACTS, new Response.Listener<String>() {
            @Override
            public void onResponse(final String response) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            if(jsonObject.has(Constants.TAG_EXAM)) {
              JSONArray examArray = jsonObject.getJSONArray(Constants.TAG_EXAM);
                                for(int i = 0; i < examArray.length(); i++) {
                                    // get all array from JSON object
                                   JSONObject seat = examArray.getJSONObject(i);
   }
                            }
                            handler.sendEmptyMessage(1);
                        } catch (JSONException e) {
                            handler.sendEmptyMessage(2);
                            e.printStackTrace();
                        } catch (NullPointerException e) {
                            handler.sendEmptyMessage(3);
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }``
        })`
  • 1
    Possible duplicate of [Android Volley + JSONObjectRequest Caching](http://stackoverflow.com/questions/16781244/android-volley-jsonobjectrequest-caching) – Dhaval Parmar Jan 26 '16 at 08:22

2 Answers2

1

Volley doesn't provide such cache tools to cache json,you can create your own cache tool using map or LruCache to make it.because your json will be little and use little memory,you can use map to save it,it will be the easiest way to make it in my opinion.

starkshang
  • 8,228
  • 6
  • 41
  • 52
  • Check this http://stackoverflow.com/questions/16781244/android-volley-jsonobjectrequest-caching/16852314#16852314 – Jalpesh Jan 27 '16 at 04:16
0

You can cache the response of volley since it's a string using SharedPreferences.