使用齐射获取listview缓存的问题

时间:2014-04-24 12:43:05

标签: android caching android-volley

由于某种原因,我无法使用齐射检索缓存响应。

这是我的片段:

RequestQueue queue = VolleySingleton.getInstance(getActivity()).getRequestQueue();

        JsonObjectRequest JOR = new JsonObjectRequest(Request.Method.GET, url, null,

                new Response.Listener<JSONObject>()
                {

                    @Override
                    public void onResponse(JSONObject response) {
                        if(getActivity()!=null && isVisible()){
                        try 
                            {
                            JSONArray posts = (JSONArray) response.getJSONArray("posts");

                            for(int i=0; i<posts.length();i++){
                                HashMap<String, String> map = new HashMap<String, String>();
                                //TAG_TITLE.add(posts.getJSONObject(i).getString("title"));
                                String title = posts.getJSONObject(i).getString(TAG_TITLE);
                                Log.d("Checking Title", "title"+TAG_TITLE);
                                String message = posts.getJSONObject(i).getString(TAG_MESSAGE);
                                String myevent_img = posts.getJSONObject(i).getString(TAG_IMG);
                                String sponser = posts.getJSONObject(i).getString(TAG_SPONSER);
                                String whoinvited = posts.getJSONObject(i).getString(TAG_WHOINVITED);
                                String location = posts.getJSONObject(i).getString(TAG_LOCATION);
                                String dresscode = posts.getJSONObject(i).getString(TAG_DRESSCODE);
                                String time = posts.getJSONObject(i).getString(TAG_TIME);
                                String endtime = posts.getJSONObject(i).getString(TAG_ENDTIME);

                                //_list.add(myevent_img);
                                 map.put(TAG_SPONSER, sponser);
                                 map.put(TAG_TITLE, title);
                                 map.put(TAG_LOCATION, location);
                                 map.put(TAG_TIME, time);
                                 map.put(TAG_ENDTIME, endtime);
                                 map.put(TAG_MESSAGE, message);
                                 map.put(TAG_WHOINVITED, whoinvited);
                                 map.put(TAG_DRESSCODE, dresscode);
                                 map.put(TAG_IMG, myevent_img);
                                Log.d("jobj ", "event_img"+myevent_img);
                                Log.d("hashmap ", "sponser "+map.get("sponser").toString());
                                eventList.add(map);
                            }
                            } 
                        catch (JSONException e) 
                            {
                            }

                            Log.d("getActivity ", "getActivity is not null");
                        adapter = new LazyAdapter(getActivity(), eventList);
                        list.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        }else
                        {
                            Log.d("getActivity ", "getActivity IS NULLL!!");

                            //list.setAdapter(null);
                            //adapter.notifyDataSetChanged();

                        }

                    }}, new Response.ErrorListener(){

                        @Override
                        public void onErrorResponse(VolleyError Error) {
                            Toast.makeText(getActivity(), Error.toString(), Toast.LENGTH_LONG).show();

                        }
                    });

        //queue.add(JOR);
        JOR.setShouldCache(true);
        Entry entry = queue.getCache().get(url);
        if(entry!=null){
            Log.d("Cached", "CACHE Is Present!!");
             try {
                String data = new String(entry.data, "UTF-8"); Toast.LENGTH_LONG).show();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else{
            queue.add(JOR);
            }

我正在使用VolleySingleton来处理缓存。 条目总是似乎是空的。我错过了任何代码。 我真正想要的是在没有网络连接的情况下让listview从缓存中检索数据。

1 个答案:

答案 0 :(得分:0)

凌空建筑为您解决这个问题。如果给定URL的响应已被Volley L2磁盘缓存缓存,则从RequestQueue请求URL将立即返回响应,而无需前往网络。从概念上讲,您不应该手动检索Volley的缓存并查看它,因为这正是Volley在返回您的响应之前所做的事情。

请注意:为了我刚才描述的缓存工作,有2个先决条件:

  1. 您必须在有网络连接时成功发出请求。这很明显 - 数据必须来自某个地方。
  2. 根据 HTTP缓存标头,Volley的默认磁盘缓存会缓存每个响应(除非另有说明)。如果没有缓存标头/缓存标头已过期,则响应将在磁盘缓存中可用。
  3. 旁注:通过Volley加载的图像也具有内存缓存的优势。

    可能的解决方案(按照最佳顺序排列):

    作为初步步骤:删除手动通过Volley缓存的代码。

    1. 无需进一步操作,因为您收到的JSON响应具有相应的缓存标头。
    2. JSON响应没有任何缓存标头,但您控制服务器 - 将相应的缓存标头添加到响应中。
    3. 手动覆盖Volley源文件中的默认缓存策略,以更好地满足您的需求,例如:始终缓存所有/缓存特定的URL /等。
    4. 维护单独的磁盘/内存缓存并管理您自己的JSON响应缓存。这可能会导致重复的缓存(你和Volley在两个不同的地方缓存相同的JSON),因此最多可达到你的应用在设备内存中占用的磁盘空间量的两倍。
相关问题