【问题标题】:Android Json Response CachingAndroid Json 响应缓存
【发布时间】:2015-04-27 14:36:34
【问题描述】:

我使用 volley 框架从服务器获取 json 响应并更新 View 元素,我使用 Volley,因为它处理缓存。

每当加载活动时,volley 总是从服务器获取响应,并且它不会从缓存的响应中加载任何数据,因此我无法使用该缓存的响应来离线加载内容。

我从服务器加载所有图像,每次 volley 从服务器获取时,都会导致响应延迟并且不会离线加载图像。

我浏览了一些 stackoverflow 的答案,但我仍然不清楚找到缓存 json 响应并使用它使应用程序脱机工作的正确方法,当设备连接到互联网时,它应该与来自的最新内容同步服务器。

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

我在这里缺少什么来缓存 json 响应?

【问题讨论】:

  • 我在同一个问题上进行了搜索,并获得了通过写入文件或将其保存在 SQLite 中来缓存来自服务器的 json 响应的解决方案。

标签: android json caching android-volley offline-caching


【解决方案1】:

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

【讨论】:

  • 你能举个例子吗?
【解决方案2】:

我最近开发了一个用于此场景的项目。 http://mbag102.github.io/spice

基本上,它利用 GS​​ON 和 Volley 来执行服务器请求并将结果缓存在 SQLite DB 中(使用稍微修改的 Sugar ORM 版本)。我在我的各种应用中都使用了它,并且证明非常快速和有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2015-02-21
    • 2015-08-21
    • 1970-01-01
    • 2020-03-25
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多