简体   繁体   中英

Android Json Response Caching

I use volley framework to fetch the json responses from the server and update the View elements, I use Volley, as it handles the caching.

Whenever activity loads, volley always fetches response from server, and it does not loading any data from cached response, so I cannot use that cached response to load content in offline.

I load all my images from server, every time volley fetches from server, causes for delayed response & doesn't load images offline.

I went through some stackoverflow answers, still I'm not clear on finding the right way to cache the json response and use it to make the app work offline, when the device connected to internet, it should sync with latest content from server.

                RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
                queue.start();
                JsonArrayRequest Req = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {    
                                JSONObject obj = response.getJSONObject(i);
                                MainPojo test = new MainPojo();
                                String name = obj.getString("facility_name");
                                MainPojo.oName.add(name);
                                test.setName(name);                                 
                                String imgUri = obj.getString("easyurl");                                   
                                System.out.println(imgUri);                                 
                                Integer in = getDrawable(imgUri);
                                Integer ap = R.drawable.appointments;                                   
                                System.out.println(ap);                                 
                                System.out.println(in);              

                                // adding movie to movies array
                                testList.add(test);

                                    } catch (JSONException e) {
                                e.printStackTrace();
                                    }    
                                }    
                        // notifying list adapter about data changes                          
                        mAdapter.notifyDataSetChanged();
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());                    

                            }
                        })
                    {            
                    //**
                    // Passing some request headers
                    //*
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));                
                        headers.put("Set-Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));                                   
                        return headers;
                    } 
                };   
                // Add the request to the RequestQueue.      
                MySingleton.getInstance(this).addToRequestQueue(Req);

What am I missing here to cache the json response?

服务器管理响应中的“缓存”标头中可以缓存的内容:“过期”或“最大年龄”

I have recently developed a project that is used for this scenerio. http://mbag102.github.io/spice .

Basically, it leverages GSON and Volley to do Server requests and caches the results in a SQLite DB (using a slightly modified version of Sugar ORM). I use it in a variety of my apps and has proved quite fast and effective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM