1.先来认识一下xUtils3
1)xUtils3简介
xUtils是目前功能比较完善的一个Android开源框架,最近又发布了xUtil3.0,在增加新功能的同时又提高了框架的性能,下面来看看官方(https://github.com/wyouflf/xUtils3)对xUtils3的介绍:
xUtils包含了很多实用的android工具;
xUtils支持超大文件(超过2G)上传,更全面的http请求协议支持(11种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响;
xUtils 最低兼容Android 4.0 (api level 14);
xUtils3变化较多所以建立了新的项目不在旧版(github.com/wyouflf/xUtils)上继续维护, 相对于旧版本:
HTTP实现替换HttpClient为UrlConnection, 自动解析回调泛型, 更安全的断点续传策略;
支持标准的Cookie策略, 区分domain, path;
事件注解去除不常用的功能, 提高性能;
数据库api简化提高性能, 达到和greenDao一致的性能;
图片绑定支持gif(受系统兼容性影响, 部分gif文件只能静态显示), webp; 支持圆角, 圆形, 方形等裁剪, 支持自动旋转。
2)在我们的项目中快速配置xUtils3
xUtils3的配置十分的简单:
2-1)使用Gradle构建时添加一下依赖即可
compile'org.xutils:xutils:3.3.36'
如果使用eclipse可以点击下面链接下载aar文件, 然后用zip解压,取出jar包和so文件。
Github下载:https://github.com/wyouflf/xUtils3
JCenter下载:http://jcenter.bintray.com/org/xutils/xutils/
Maven下载1:http://central.maven.org/maven2/org/xutils/xutils/
Maven下载2:http://repo1.maven.org/maven2/org/xutils/xutils/
2-2)加入权限

2-3)创建Application

xUtils3主要包含注解模块、网络模块、图片模块和数据库模块,下面将做一一说明。
xUtils3注解模块在实际开发中的使用如下:
1)Activity的注解的使用如下:

2)Fragment的注解的使用如下:

3)为按钮设置点击事件
方法必须私有限定,
方法参数形式必须和type对应的Listener接口一致.
注解参数value支持数组: value={id1, id2, id3}

强调:使用注解模块一定要注意初始化视图注解框架。
xUtils3网络模块大大方便了在实际开发中网络模块的开发,xUtils3网络模块大致包括GET请求、POST请求、如何使用其他请求方式、上传文件、下载文件、使用缓存等功能,下面将做一一说明:
1)GET请求

下面我们来看下带有缓存的GET请求,POST请求和其他请求方式类似,后面就不再赘述:
带有缓存的GET请求:

上面onCache方法中需要注意的几点:
a)如果服务端没有返回过期时间,参考params.setCacheMaxAge(maxAge)方法。
b)客户端会根据服务端返回的header中max-age或expires来确定本地缓存是否给onCache方法。如果服务端没有返回max-age或expires,那么缓存将一直保存,除非这里自己定义了返回false,那么xUtils将请求新数据, 来覆盖它。
c)如果信任该缓存返回true,将不再请求网络。返回false继续请求网络,但会在请求头中加上ETag,Last-Modified等信息。如果服务端返回304,则表示数据没有更新,不继续加载数据。
2)POST请求

3)其他网络请求方式

4)上传文件

5)下载文件

xUtils3图片模块,重点在于加载图片的4个bind方法,loadDrawable与loadFIle用法和ImageOptions用法,需多加练习。
1)xUtils3 ImageOptions:
12

2)xUtils3 bind方法:

3)xUtils3 loadDrawable方法:

4)xUtils3 loadFile方法:
当我们通过bind()或者loadDrawable()方法加载了一张图片后,它会保存到本地文件中,那当我需要这张图片时,就可以通过loadFile()方法进行查找。

1)初始化配置和创建实体类
首先在项目Application中进行初始化配置DaoConfig(与onCreate方法同级目录下):

然后创建数据库表ChildInfo的实体类:

之后就能进行创建和删除数据库的操作了:
2)创建数据库

3)删除数据库
db.dropDb();
4)删除表
db.dropTable(ChildInfo.class);
5)新增表中的数据
ChildInfo childInfo =newChildInfo("zhangsan123");db.save(childInfo);
6)删除表中的数据
//第一种写法:
db.delete(ChildInfo.class);//child_info表中数据将被全部删除
//第二种写法,添加删除条件:
WhereBuilder b = WhereBuilder.b();
b.and("id",">",2);//构造修改的条件
b.and("id","<",4);
db.delete(ChildInfo.class, b);
7)修改表中的数据

8)查询表中的数据

具体查看api
1)UI异步执行

2)UI同步执行

具体参考github地址:https://github.com/wyouflf/xUtils3
网友评论