@liter
2015-10-23T14:49:09.000000Z
字数 5227
阅读 13717
litehttp2.x版本系列教程
QQ群: 大群 47357508 , 二群 42960650
本系列文章面向android开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。
本系列文章目录总览: https://zybuluo.com/liter/note/186513
初始化LiteHttp需要传入一个HttpConfig的实例来配置各项参数,若传入null即表示使用全部使用默认设置。
需要注意的是,一个App仅需要构建一个LiteHttp的实例即可,即单例模式,这样才能最节省系统资源,多个实例并不提升效率反而浪费资源。
使用默认配置,不是我良辰不给你,你不是很屌吗,自力更生吧:
LiteHttp liteHttp = LiteHttp.newApacheHttpClient(null);
更推荐简单的自定义配置:
HttpConfig config = new HttpConfig(activity) // configuration quickly.setDebugged(true) // log output when debugged.setDetectNetwork(true) // detect network before connect.setDoStatistics(true) // statistics of time and traffic.setUserAgent("Mozilla/5.0 (...)") // set custom User-Agent.setTimeOut(10000, 10000); // connect and socket timeout: 10sliteHttp = LiteHttp.newApacheHttpClient(config);
这个案例示范了context(用于网络状态判断),打开调试,探测网络状态,开启数据统计,User-Agent,以及连接和读取超时等参数的配置。
更多的配置项有达23+项之多,比较灵活,后边会有专门章节详细说明。
下面讲解如何发起请求,我们首先定义合法的http地址,如:
String url = "http://baidu.com";String picUrl = "http://pic.33.la/20140403sj/1638.jpg";
撸一行代码即可,节省的时间爷沏一杯菊花茶:
String html = liteHttp.perform(new StringRequest(url));
构建一个请求,通过litehttp执行获取结果,当然无需回调,直截了当。
再撸一行,获取一张美图:
Bitmap bitmap = liteHttp.perform(new BitmapRequest(picUrl));
注意:同步请求在当前线程直接执行,应用需开启子线程执行。
异步请求获取String:
liteHttp.executeAsync(new StringRequest(url));
异步获取原始byte:
liteHttp.executeAsync(new BytesRequest(url));
异步加载一张位图:
String saveToPath = "/sdcard/a.png";liteHttp.executeAsync(new BitmapRequest(url,saveToPath));
saveToPath用来输入一个你指定的文件位置,位图将会保存到这里,传入null则保存默认位置。
异步下载一个文件:
liteHttp.executeAsync(new FileRequest(url,saveToPath));
saveToPath 用来输入一个你指定的文件位置,文件将会保存到这里,传入null则保存缓存默认位置。
文件默认存储位置和命名规则:
- 统一存放到 HttpConfig 设置指定的文件夹中。
- 请求已设置 Cache-Key,则取其为文件名字。
- 请求未设置 Cache-Key,则通过处理 Url 自动生成文件名字。
我们知道了怎么发起异步请求,那么请求是成功还是失败,成功如何获取结果,失败如何获取异常?
同步 获取 String,在 UI线程 处理结果或异常:
// 主线程处理,注意HttpListener默认是在主线程回调// get data in listener, handle result on UI threadliteHttp.execute(new StringRequest(url).setHttpListener(new HttpListener<String>() {@Overridepublic void onSuccess(String data, Response<String> response) {response.printInfo();}}));
异步 下载 File,在 UI线程 监听进度:
liteHttp.executeAsync(new FileRequest(url,saveToPath).setHttpListener(new HttpListener<File>(true, true, true) {@Overridepublic void onLoading(AbstractRequest<Bitmap> request, long total, long len) {// 进度通知}}));
同步 加载位图,在 当前线程 监听HttpListener构造函数第一个参数设置为false即可;第二、三个参数设置为true开启上传、下载进度监听:
// 2.2 return data directly, and handle result on current thread(当前主线程处理)Bitmap bitmap = liteHttp.perform(new BitmapRequest(picUrl).setHttpListener(new HttpListener<Bitmap>(false, true, true) {@Overridepublic void onLoading(AbstractRequest<Bitmap> request, long total, long len) {// down loading notification ...}@Overridepublic void onUploading(AbstractRequest<Bitmap> request, long total, long len) {// up loading notification...}}));
异步 获取 String,在 当前线程 处理结果,HttpListener构造函数参数设置为false即可:
liteHttp.executeAsync(new StringRequest(url).setHttpListener(new HttpListener<String>(false) {@Overridepublic void onSuccess(String s, Response<String> response) {// 成功:主线程回调,反馈一个stringresponse.printInfo();}@Overridepublic void onFailure(HttpException e, Response<String> response) {// 失败:主线程回调,反馈异常response.printInfo();}}));
其他模式的请求用法一样,这里不再多举。
请求默认是GET方式,假设完成一个登陆操作,API和参数为http://xxx?name=lucy&password=123456,登陆成功返回User对象,我们有以下接口:
public String loginUrl = "http://litesuits.com/mockdata/user_get";
该接口的响应体是个Json字符串:
{"api": "com.xx.get.userinfo","v": "1.0","result": {"code": 200,"message": "success"},"data": {"age": 18,"name": "qingtianzhu","girl_friends": ["xiaoli","fengjie","lucy"]}}
根据该Json响应,对应的 User 类如下:
public class User {private String api;private String v;protected int code;protected String message;protected Data data;public static class Data {public int age;public String name;public ArrayList<String> girl_friends;}}
通过注解约定的方式配置代码如下,更清晰:
// 1.1 地址:loginUrl// 1.2 参数:name=value&password= value// 1.3 响应:成功返回 User 对象@HttpUri(loginUrl)class LoginParam extends HttpRichParamModel<User> {private String name;private String password;public LoginParam(String name, String password) {this.name = name;this.password = password;}}// 2. 一句话调用即实现登陆和监听liteHttp.executeAsync(new LoginParam("lucy", "123456").setHttpListener(new HttpListener<User>() {@Overridepublic void onSuccess(User user, Response<User> response) {HttpUtil.showTips(activity, "对象自动转化", user.toString());}}));
LoginParam 一个类定义了 API地址、参数、响应体 三个关键数据定义,非常的清晰简单。
HttpListener各个回调的时机有:开始、成功、失败、取消、重试、上传进度、下载进度、结束。
值得注意的是 HttpListener 有多个参数(构造函数和使用set方法均可)可以设置:
HttpListener(boolean runOnUiThread, boolean readingNotify, boolean uploadingNotify)
分别用于设置:
一般情况下:
其他更多最佳实践方式在请关注另外分析文章。