LiteHttp 第六节:禁用网络和流量&耗时统计
来源:互联网 发布:欧时力淘宝代购2017 编辑:程序博客网 时间:2024/05/29 12:38
官网: http://litesuits.com
QQ群: 大群 47357508,二群 42960650
本系列文章面向android开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。
本系列文章目录总览: http://blog.csdn.net/ko33600/article/details/49367409
1. 禁用网络
开发者可以通过lite-http的config来设置禁用某种网络,比如我们将移动网络和wifi都禁用:
// disable some networkHttpConfig config = liteHttp.getConfig();// must set contextconfig.setContext(activity);// disable mobile(2G/3G/4G) and wifi networkconfig.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_MOBILE | HttpConfig.FLAG_NET_DISABLE_WIFI);liteHttp.executeAsync(new StringRequest(url).setHttpListener(new HttpListener<String>() { @Override public void onSuccess(String s, Response<String> response) { HttpUtil.showTips(activity, "LiteHttp2.0", s); } @Override public void onFailure(HttpException e, Response<String> response) { HttpUtil.showTips(activity, "LiteHttp2.0", e.toString()); }}));
这样不管是sim卡网络还是无线wifi下发送请求都会失败,onFailure会触发。
恢复可用状态:
// enable networkHttpConfig config = liteHttp.getConfig();// enable all config.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_NONE);
2. 流量和耗时统计
只需要从配置打开统计开关即可:
// Traffic/Time StatisticsString picUrl = "http://pic.33.la/20140403sj/1638.jpg";// turn onliteHttp.getConfig().setDoStatistics(true);liteHttp.executeAsync(new FileRequest(picUrl).setHttpListener(new HttpListener<File>() { @Override public void onSuccess(File file, Response<File> response) { String msg = "This request take time:" + response.getUseTime() + ", readed length:" + response.getReadedLength(); msg += " Global " + liteHttp.getStatisticsInfo(); HttpUtil.showTips(activity, "LiteHttp2.0", msg); } @Override public void onFailure(HttpException e, Response<File> response) { HttpUtil.showTips(activity, "LiteHttp2.0", e.toString()); }}));
更多数据统计:
response.getRedirectTimes(); // 重定向的次数
response.getRetryTimes(); // 重试的次数
response.getUseTime(); // 本次请求耗时
response.getContentLength(); // header中的数据长度(Content-Length)
response.getReadedLength(); // 实际读取的数据长度
整体数据统计:
StatisticsInfo sta = liteHttp.getStatisticsInfo();
sta.getConnectTime(); // litehttp 实例化后所有请求耗时累计
sta.getDataLength(); // litehttp 实例化后读取数据长度累计
- LiteHttp 第六节:禁用网络和流量&耗时统计
- LiteHttp 第七节:重试和重定向
- Android网络通信框架LiteHttp:简介和教学大纲
- Linux 下统计高速网络中的流量
- Linux_常用命令耗时统计
- 统计代码耗时长短
- python 耗时效率统计
- Delphi 时间耗时统计
- iOS 启动耗时统计
- ios 代码耗时统计
- 流量统计
- 流量统计
- 流量统计
- 流量统计
- 流量统计
- 流量统计
- 流量统计
- 流量统计
- LiteHttp 第五节:文件、位图的上传和下载
- 毛笔日常保养注意事项
- [PAT (Advanced Level) ]1014.Waiting in Line 解题文档
- java中Builder设计模式 原理和好处
- Windbg跟踪临界区的BUG
- LiteHttp 第六节:禁用网络和流量&耗时统计
- [深入理解Java虚拟机]第七章 类加载的时机
- Minimum Size Subarray Sum 数组
- LiteHttp 第七节:重试和重定向
- php中Password Hashing加密方法详解
- navicat初步使用
- 两个队列实现栈
- C++那些细节--函数指针
- 小学四则运算演算程序