使用Volley库缓存文本和图像

时间:2014-08-25 16:14:50

标签: android android-volley

用于android中网络操作的Volley库的一个功能是它将文本和图像缓存到磁盘中,因此通过更改方向,应用程序不必再次下载图像和文本。这段代码使用了这个功能:

public class AppsActivity extends Activity {

static public Typeface font;
List<Application> appsList;
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> description = new ArrayList<String>();
AppAdapter adapter;
GridView gv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apps);
    font =Typeface.createFromAsset(getAssets(), "fonts/BMitra.ttf");
    appsList = new ArrayList<Application>();
    gv = (GridView) findViewById(R.id.gridView1);
    adapter = new AppAdapter(this, appsList);
    gv.setAdapter(adapter);
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Entry entry = cache.get("http://appline.ir/index.php/android.feed?limitstart=");
    if (entry != null)
    {
        fetch the data from cache
        try {
            String data = new String(entry.data, "UTF-8");
            parseXmlString(data);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

    } 
    else 
    {
        StringRequest request = new StringRequest(Method.GET, "http://appline.ir/index.php/android.feed?limitstart=", new Listener<String>() {

            @Override
            public void onResponse(String res)
            {
                parseXmlString(res);
            }

    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            Intent intent = new Intent(AppsActivity.this,
                    AppDetailActivity.class);

            Application app;
            app=appsList.get(position);
            intent.putExtra("title", app.getTitle());
            intent.putExtra("imageUrl", app.getImageUrl());
            intent.putExtra("description", description.get(position+1));
            //              intent.putExtra("title", app.getTitle());

            startActivity(intent);
        }

    });

}

public void parseXmlString(String string)
{

    XmlPullParserFactory parser;
    XmlPullParser xml = null;

    try
    {
        parser =XmlPullParserFactory.newInstance();
        xml = parser.newPullParser();
        StringReader reader = new StringReader(string);
        xml.setInput(reader);
    }   
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    }

    int event;
    String text = null;
    try {

        event = xml.getEventType();
        while (event != XmlPullParser.END_DOCUMENT)
        {
            String name=xml.getName();
            switch (event)
            {
            case XmlPullParser.START_TAG:
                break;
            case XmlPullParser.TEXT:
                text = xml.getText();
                break;
            case XmlPullParser.END_TAG:
                if(name.equals("title")){
                    title.add(text);
                }
                else if(name.equals("link")){   
                    //                        link = text;
                }
                else if(name.equals("description")){

                    description.add(text);
                }
                else{
                }
                break;
            }        
            event = xml.next(); 
        }
    } 
    catch (Exception e) {
        System.err.println(e.getMessage());
    }
    System.out.println(description.size());
    for(int i = 1; i < description.size(); i++)
    {
        Document doc = Jsoup.parse((description.get(i)));
        Elements links = doc.select("p"); // a with href
        Elements pngs = links.select("img[src$=.png]");
        String image = pngs.attr("src");
        appsList.add(new Application(Html.fromHtml(title.get(i)).toString(), image, font));
    }
    adapter.notifyDataSetChanged();
}

我的问题是,当我调试代码时(在if子句中使用System.out进行缓存),我发现我们从来没有得到缓存命中。怎么了?

1 个答案:

答案 0 :(得分:1)

服务器不允许您缓存这些文件,为了确认我的答案,您可以执行以下操作之一:

  1. 为mozilla下载此插件(RESTClient)并发送您的请求并检查缓存控制的头文件

  2. headerValue = headers.get("Cache-Control");班级的HttpHeaderParse设置断点,看看情况如何。

  3. 所有这些解决方案仅在您将请求设置为可兑现shouldCache();

    时才有效

    并且为了更快地接受我的回答,请查看缓存控制: - )

    enter image description here

相关问题