Android实现离线缓存的方法

2019-12-10 18:43:13王振洲
word", "123456"); return map; } }; queue.add(stringRequest); }

当接口访问成功以后,Volley会自动缓存此次纪录在/data/data/{package name}/cache/volley文件夹中。

Android,离线缓存

打开上面的文件,可以发现接口的路径和返回值都被保存在该文件里面了。

Android,离线缓存

当在断网状态时,如何获取到该接口的缓存的返回值呢? 
使用RequestQueue提供的getCache()方法查询该接口的缓存数据

if (queue.getCache().get(TEST_API) != null) {
String cachedResponse = new String(queue.getCache().get(TEST_API).data);

2.使用Universal-ImageLoader加载图片

ImageLoader.getInstance().displayImage(head_img, imageView);

注意点