- 1、
Default
模式,也是没有设置缓存模式时的默认模式 这个模式实现http协议中的内容,比如响应码是304时,当然还会结合E-Tag和LastModify等头。
StringRequest request = new StringRequest(url, method);
request.setCacheMode(CacheMode.DEFAULT);
- 2、 当请求服务器失败的时候,读取缓存 请求服务器成功则返回服务器数据,如果请求服务器失败,读取缓存数据返回。
StringRequest request = new StringRequest(url, method);
request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
- 3、如果发现有缓存直接成功,没有缓存才请求服务器 ImageLoader的核心除了内存优化外,剩下一个就是发现把内地有图片则直接使用,没有则请求服务器。
请求String
,缓存String
:
StringRequest request = new StringRequest(url, method);
// 非标准Http协议,改变缓存模式为IF_NONE_CACHE_REQUEST_NETWORK request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);
请求图片,缓存图片:
ImageRequest request = new ImageRequest(url, method);
request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);
- 4、仅仅请求网络 无论如何也只会请求网络,也不支持http 304这种默认行为。
ImageRequest request = new ImageRequest(url, method);
request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK); ...
- 5、仅仅读取缓存 无论如何仅仅读取缓存,不会请求网络和其它操作。
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_READ_CACHE);
注意:如果开发者想先得到缓存再请求网络,开发者可以先发起一个仅仅读取缓存的Request
,然后发起一个仅仅请求网络的Request
,不过本人已经在准备NoHttp2.0了,到时候将会以一个全新的面貌和开发者们见面。
缓存模式支持缓存任何数据,因为NoHttp
保存数据是转为byte[]
,读取数据时是把byte[]
转为开发者想要的数据,因此NoHttp
的缓存可以支持任何自定义的Request
。
服务器端:
1 @WebServlet("/cache") 2 public class CacheServlet extends BaseJsonServlet { 3 private static final long serialVersionUID = 14646L; 4 5 public CacheServlet() { 6 super(); 7 } 8 9 @Override 10 protected String onResponse(HttpServletRequest request, HttpServletResponse response) throws Exception { 11 System.out.println("返回新的数据"); 12 return "NoHttp是最好用的Android网络框架。"; 13 } 14 15 /** 16 * 服务端本接口的数据是否过期,没有过期则反悔相应头304,如果过期,会重新返回数据 17 */ 18 @Override 19 protected long getLastModified(HttpServletRequest req) { 20 // 这里主要是告诉http框架我们的数据是否被修改过,或者说是否过期 21 String path = getServletContext().getRealPath("index.html"); 22 return new File(path).lastModified(); 23 } 24 25 }
客户端:
1 public class CacheActivity extends Activity implements View.OnClickListener { 2 3 /** 4 * 标志请求是一般协议下的 5 */ 6 private final int nohttp_what_org = 0x01; 7 /** 8 * 标志请求是请求失败时读取缓存 9 */ 10 private final int nohttp_what_failed_read_cache = 0x02; 11 /** 12 * 标志请求是仅仅读取缓存的 13 */ 14 private final int nohttp_what_only_read_cache = 0x03; 15 /** 16 * 测试缓存图片 17 */ 18 private final int nohttp_what_only_read_cache_image = 0x04; 19 20 /** 21 * 显示请求数据 22 */ 23 private TextView mTvResult; 24 /** 25 * 显示请求图片 26 */ 27 private ImageView mIvImage; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_cache); 33 findViewById(R.id.btn_request_org_cache).setOnClickListener(this); 34 findViewById(R.id.btn_request_failed_read_cache).setOnClickListener(this); 35 findViewById(R.id.btn_request_none_cache_request).setOnClickListener(this); 36 findViewById(R.id.btn_request_only_read_cache).setOnClickListener(this); 37 findViewById(R.id.btn_request_failed_read_cache_image).setOnClickListener(this); 38 mTvResult = (TextView) findViewById(R.id.tv_result); 39 mIvImage = (ImageView) findViewById(R.id.iv_image_cache); 40 } 41 42 @Override 43 public void onClick(View v) { 44 if (v.getId() == R.id.btn_request_org_cache) { 45 // 一般请求,走http标准协议 46 String url = "http://192.168.1.116/HttpServer/cache"; 47 Request<JSONObject> request = new FastJsonRequest(url); 48 // 因为NoHttp本身就是RESTFUL风格的标准Http协议,所以这里不用设置或者设置为DEFAULT 49 request.setCacheMode(CacheMode.DEFAULT); 50 CallServer.getInstance().add(this, request, callBack, nohttp_what_org, true, false, true); 51 } else if (v.getId() == R.id.btn_request_failed_read_cache) { 52 // 请求失败的时候返回缓存 53 String url = "http://192.168.1.116/HttpServer/cache"; 54 Request<JSONObject> request = new FastJsonRequest(url); 55 // 非Http的标准协议,需要设置为REQUEST_FAILED_READ_CACHE 56 request.setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE); 57 CallServer.getInstance().add(this, request, callBack, nohttp_what_failed_read_cache, true, false, true); 58 } else if (v.getId() == R.id.btn_request_none_cache_request) { 59 // 如果没有缓存才去请求服务器,否则使用缓存 60 String url = "http://192.168.1.116/HttpServer/cache"; 61 Request<JSONObject> request = new FastJsonRequest(url); 62 // 非Http的标准协议,需要设置为IF_NONE_CACHE_REQUEST 63 request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST); 64 CallServer.getInstance().add(this, request, callBack, nohttp_what_only_read_cache, true, false, true); 65 } else if (v.getId() == R.id.btn_request_only_read_cache) { 66 // 仅仅请求缓存,不请求服务器 67 String url = "http://192.168.1.116/HttpServer/cache"; 68 Request<JSONObject> request = new FastJsonRequest(url); 69 // 非Http的标准协议,需要设置为ONLY_READ_CACHE 70 request.setCacheMode(CacheMode.ONLY_READ_CACHE); 71 CallServer.getInstance().add(this, request, callBack, nohttp_what_only_read_cache, true, false, true); 72 } else if (v.getId() == R.id.btn_request_failed_read_cache_image) { 73 // 如果没有缓存才去请求服务器,否则使用缓存,缓存图片演示,这一点非常适合封装一个自己的Imageloader是来使用 74 Request<Bitmap> request = NoHttp.createImageRequest("http://image.tianjimedia.com/uploadImages/2013/214/CN267OUS22LM.jpg"); 75 request.setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE); 76 CallServer.getInstance().add(this, request, imageBack, nohttp_what_only_read_cache_image, true, false, true); 77 } 78 } 79 80 /** 81 * 结束图片的请求结果 82 */ 83 private HttpCallBack<Bitmap> imageBack = new HttpCallBack<Bitmap>() { 84 @Override 85 public void onSucceed(int what, Response<Bitmap> response) { 86 mIvImage.setImageBitmap(response.get()); 87 mTvResult.setText("请求成功,是否来自缓存:" + response.isFromCache()); 88 } 89 90 @Override 91 public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) { 92 mIvImage.setImageResource(R.drawable.ic_launcher); 93 mTvResult.setText("请求失败"); 94 } 95 }; 96 97 /** 98 * 处理JSONObject的请求结果 99 * 100 * @param what 是哪个请求 101 * @param response 请求对应的响应结果 102 */ 103 private void handlerResponse(int what, Response<JSONObject> response) { 104 String result = null; 105 if (what == nohttp_what_org) { 106 result = "一般请求,"; 107 } else if (what == nohttp_what_failed_read_cache) { 108 result = "请求失败时返回缓存,"; 109 } else if (what == nohttp_what_only_read_cache) { 110 result = "仅仅请求缓存,"; 111 } 112 result += "请求是否来缓存" + response.isFromCache() + ";结果是:" + response.get().getString("data"); 113 mTvResult.setText(result); 114 } 115 116 /** 117 * 接受JSONObject的请求结果 118 */ 119 private HttpCallBack<JSONObject> callBack = new HttpCallBack<JSONObject>() { 120 121 @Override 122 public void onSucceed(int what, Response<JSONObject> response) { 123 handlerResponse(what, response); 124 } 125 126 @Override 127 public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) { 128 mTvResult.setText("请求失败"); 129 } 130 }; 131 132 }