摘要:请求,缓存非标准协议,改变缓存模式为请求图片,缓存图片仅仅请求网络无论如何也只会请求网络,也不支持这种默认行为。仅仅读取缓存无论如何仅仅读取缓存,不会请求网络和其它操作。
Default
模式,也是没有设置缓存模式时的默认模式 这个模式实现http协议中的内容,比如响应码是304时,当然还会结合E-Tag和LastModify等头。StringRequest request = new StringRequest(url, method); request.setCacheMode(CacheMode.DEFAULT);
StringRequest request = new StringRequest(url, method); request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
请求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);
ImageRequest request = new ImageRequest(url, method); request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK); ...
Requestrequest = 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 HttpCallBackhttpCallBack = new HttpCallBack () { 43 @Override 44 public void onSucceed(int what, Response 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() + "n数据:"; 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 imageHttpCallBack = new HttpCallBack () { 65 @Override 66 public void onSucceed(int what, Response 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 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 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 }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/3072.html
摘要:登陆退出处理登录结果登录接口数据处理登出结果退出接口数据请求失败添加一个请求到请求队列上下文请求对象接受回调结果,当多个请求用同一个接受结果时,用来区分请求是否显示请求是否能被用户取消是否提示用户错误信息客户端错误客户端发生错误服务器错误服NoHttpActivity 1 public class NoHttpActivity extends Activity implements Vi...
摘要:下载下载按钮暂停开始等下载状态下载进度条下载地址下载请求下载地址保存的文件夹文件名是否断点续传下载如果发现文件已经存在是否删除后重新下载检查已经下载了一半的文件是什么状态文件已经下载完成下载完成已经下载完成提示用户安装代表文件不存在,需要从xml java: 1 import com.yolanda.nohttp.Headers; 2 ...
摘要:实体类请求针对的具体在中使用处理登录结果登录接口结果处理登出结果退出接口结果请求失败可以解析所有的自定义请求但是前提是传进来的必须提供了默认实现bean实体类请求: 1.bean 1 import java.io.Serializable; 2 import com.alibaba.fastjson.annotation.JSONField; 3 4 public class U...
摘要:程序入口第一种,先进先出的队列第二种,没有顺序的队列往队列中添加请求请求是一个先进先出的队列如果方法不做比较返回,那么是无序的方法是一个阻塞的方法,每次调用会拿到队列中的第一个任务,如果队列为空,这个方法将一直阻塞,知道队列中有任务再次返回public class Main { /** * 程序入口 */ public void start() { ...
阅读 2807·2021-09-10 10:50
阅读 2741·2021-09-04 16:45
阅读 3053·2019-08-30 14:19
阅读 3407·2019-08-29 17:31
阅读 3115·2019-08-29 16:43
阅读 2017·2019-08-29 14:05
阅读 1979·2019-08-29 13:17
阅读 1930·2019-08-26 13:25