【问题标题】:Android - ION caches resultAndroid - ION 缓存结果
【发布时间】:2014-04-17 10:07:30
【问题描述】:

我目前正在编写一个小应用程序,通过下载 last.fm 生成的 XML 文件来显示当前在本地酒吧播放的歌曲。

我遇到的问题如下:与 xml 在线同步时,它不会获得新版本,而是一遍又一遍地使用第一个下载的 xml。同时,在随机浏览器中打开此链接确实会给出正确的结果。可能是缓存或延迟下载,我不知道。我也不知道这是否与ION有关。

我目前已经用一些代码解决了这个问题,这些代码在下载之前清除了这个应用程序的整个缓存,效果很好,但是因为我可能想要扩展应用程序,所以我必须找到另一种方法来解决这个问题。

我的代码:

public class MainActivity extends Activity implements OnClickListener {

private final static String nonXML = {the url to my xml-file}

private String resultXml;

private TextView artistTextView, songTextView, albumTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    artistTextView = (TextView) findViewById(R.id.artistTextView);
    songTextView = (TextView) findViewById(R.id.songTextView);
    albumTextView = (TextView) findViewById(R.id.albumTextView);
    Button mainButton = (Button) findViewById(R.id.mainButton);

    mainButton.setOnClickListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    update();
}

@Override
public void onClick(View v) {
    update();
}

private void update() {
    deleteCache(this);
    getXML();

    XMLToClass convertor = new XMLToClass();
    NonPlaylist non = convertor.convert(resultXml);

    artistTextView.setText(non.getArtist());
    songTextView.setText(non.getSong());
    albumTextView.setText(non.getAlbum());
}

private void getXML() {
    try {
        Ion.with(getBaseContext(), nonXML)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        resultXml = result;
                    }
                }).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}
}

【问题讨论】:

    标签: android caching last.fm android-ion


    【解决方案1】:

    根据 http 规范,Ion 实际上会缓存。如果您想忽略缓存,请在构建请求时使用 .noCache() 方法。

    提示:您还可以在 ion 请求中打开详细日志记录,以查看在缓存等方面发生了什么。

    .setLogging("MyTag", Log.VERBOSE)

    【讨论】:

    • 像魅力一样工作!谢谢!
    • Ion 缓存请求多长时间?应用实例的生命周期?缓存的大小有限制吗?如何清除缓存?
    • 如何定义缓存?如果我尝试访问与几分钟前相同的服务,我希望从缓存中获得一些结果......
    • Ion 根据 http 响应中的缓存头进行缓存。可以使用以下方法清除缓存:Ion.getDefault(getContext()) .configure().getResponseCache() .clear();
    • 嗨,首先是伟大的库什库!我可以设置需要维护缓存多长时间的配置吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2016-01-27
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多