gpt4 book ai didi

android - 使用 Volley 库缓存文本和图像

转载 作者:行者123 更新时间:2023-11-30 02:42:56 27 4
gpt4 key购买 nike

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. 为 mozilla 下载此插件 (RESTClient) 并发送您的请求并检查缓存控制的头文件

  2. HttpHeaderParse 类的 headerValue = headers.get("Cache-Control"); 中设置断点,看看发生了什么。

所有这些解决方案只有在您将请求设置为可兑现时才有效 shouldCache();

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

enter image description here

关于android - 使用 Volley 库缓存文本和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com