离线缓存就是在网络畅通的情况下将从服务器收到的数据保存到本地,当网络断开之后直接读取本地文件中的数据。如Json 数据缓存到本地,在断网的状态下启动APP时读取本地缓存数据显示在界面上,常用的APP(网易新闻、知乎等等)都是支持离线缓存的,这样带来了更好的用户体验。
如果能够在调用网络接口后自动缓存返回的Json数据,下次在断网状态下调用这个接口获取到缓存的Json数据的话,那该多好呢?Volley做到了这一点。
因此,今天这篇文章介绍的就是使用Volley自带的数据缓存,配合Universal-ImageLoader的图片缓存,实现断网状态下的图文显示。
实现效果
如何实现?
1.使用Volley访问网络接口
/**
* 获取网络数据
*/
private void getData() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, TEST_API, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
textView.setText("data from Internet: " + s);
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray resultList = jsonObject.getJSONArray("resultList");
JSONObject JSONObject = (org.json.JSONObject) resultList.opt(0);
String head_img = JSONObject.getString("head_img");
ImageLoader.getInstance().displayImage(head_img, imageView);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("phone", "15962203803");
map.put("pass











