zoukankan      html  css  js  c++  java
  • NoHttp封装--04 缓存

    • 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     private HttpCallBack<JSONObject> httpCallBack = new HttpCallBack<JSONObject>() {
     43         @Override
     44         public void onSucceed(int what, Response<JSONObject> response) {
     45             JSONObject jsonObject = response.get();
     46             String result = "";
     47             if (what == nohttp_what_org) {
     48                 result += "标准协议,";
     49             } else if (what == nohttp_what_failed_read_cache) {
     50                 result += "请求失败的时候显示缓存,";
     51             } else if (what == nohttp_what_only_read_cache) {
     52                 result += "没有缓存时请求服务器,";
     53             }
     54             result += "是否来自缓存:" + response.isFromCache() + "
    数据:";
     55             result += jsonObject.getString("data");
     56             mTvResult.setText(result);
     57         }
     58 
     59         @Override
     60         public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
     61         }
     62     };
     63 
     64     private HttpCallBack<Bitmap> imageHttpCallBack = new HttpCallBack<Bitmap>() {
     65         @Override
     66         public void onSucceed(int what, Response<Bitmap> response) {
     67             if (what == nohttp_what_only_read_cache_image) {
     68                 Bitmap bitmap = response.get();
     69                 mIvImage.setImageBitmap(bitmap);
     70                 mTvResult.setText("是否来自缓存:" + response.isFromCache());
     71             }
     72         }
     73 
     74         @Override
     75         public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
     76         }
     77     };
     78 
     79     @Override
     80     public void onClick(View v) {
     81         String url = "http://192.168.1.116/HttpServer/news";
     82         Request<JSONObject> request = new FastJsonRequest(url);
     83         if (v.getId() == R.id.btn_request_org_cache) {
     84             // 一般请求,走http标准协议
     85             url = "http://192.168.1.116/HttpServer/cache";
     86             request = new FastJsonRequest(url);
     87             request.setCacheMode(CacheMode.DEFAULT);// DEFAULT表示走Http标准协议,默认就是,这里可以不用设置
     88             CallServer.getInstance().add(this, request, httpCallBack, nohttp_what_org, true, false, true);
     89         } else if (v.getId() == R.id.btn_request_failed_read_cache) {
     90             // 请求失败的时候返回缓存
     91             request.setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE);// REQUEST_FAILED_READ_CACHE表示走请求失败的时候读取缓存
     92             CallServer.getInstance().add(this, request, httpCallBack, nohttp_what_org, true, false, true);
     93         } else if (v.getId() == R.id.btn_request_none_cache_request) {
     94             // 如果没有缓存才去请求服务器,否则使用缓存
     95             request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST);// IF_NONE_CACHE_REQUEST表示没有缓存的时候去请求服务器
     96             CallServer.getInstance().add(this, request, httpCallBack, nohttp_what_org, true, false, true);
     97         } else if (v.getId() == R.id.btn_request_only_read_cache) {
     98             // 仅仅请求缓存,不请求服务器
     99             url = "http://192.168.1.116/HttpServer/only";
    100             request = new FastJsonRequest(url);
    101             request.setCacheMode(CacheMode.ONLY_READ_CACHE);// ONLY_READ_CACHE表示仅仅请求缓存,不请求服务器
    102             CallServer.getInstance().add(this, request, httpCallBack, nohttp_what_org, true, false, true);
    103         } else if (v.getId() == R.id.btn_request_failed_read_cache_image) {
    104             // 如果没有缓存才去请求服务器,否则使用缓存,缓存图片演示,这一点非常适合封装一个自己的Imageloader是来使用
    105             String imageUrl = "http://gtb.baidu.com/HttpService/get?p=dHlwZT1pbWFnZS9qcGVnJm49dmlzJnQ9YWRpbWcmYz10YjppZyZyPTMwMjEwODc5MTEsMTQxMDg4MDEwNgAAAA==";
    106             Request<Bitmap> imageRequest = NoHttp.createImageRequest(imageUrl);
    107             imageRequest.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST);
    108             CallServer.getInstance().add(this, imageRequest, imageHttpCallBack, nohttp_what_only_read_cache_image, true, false, true);
    109         }
    110     }
    111 
    112 }

  • 相关阅读:
    python!让人惊讶的python
    由测试中的版本同步联想到敏捷开发中的两个实践
    python中比较两个文件是否相同
    python中根据类名生成类的实例
    使用sqlServer开发应用程序时要注意的10件事
    python版的Hello World
    其他语言的.net实现列表
    Bug管理的流程和几个重点
    IL Reference
    Delphi CreateProcess函数调用示例
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/9030481.html
Copyright © 2011-2022 走看看