LiteHttp 第六节:禁用网络和流量&耗时统计

官网: http://litesuits.com
QQ群: 大群 47357508,二群 42960650

本系列文章面向android开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。

本系列文章目录总览: http://blog.csdn.net/ko33600/article/details/49367409


1. 禁用网络

开发者可以通过lite-http的config来设置禁用某种网络,比如我们将移动网络和wifi都禁用:

// disable some network
HttpConfig config = liteHttp.getConfig();

// must set context
config.setContext(activity);

// disable mobile(2G/3G/4G) and wifi network
config.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 network
HttpConfig config = liteHttp.getConfig();

// enable all  
config.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_NONE);

2. 流量和耗时统计

只需要从配置打开统计开关即可:

// Traffic/Time Statistics
String picUrl = "http://pic.33.la/20140403sj/1638.jpg";

// turn on
liteHttp.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 实例化后读取数据长度累计

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值