一、xUtils3简介

  • xUtils 包含了很多实用的android工具.
  • xUtils 支持超大文件(超过2G)上传,更全面的http请求协议支持(11种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响...
  • xUtils 最低兼容Android 4.0 (api level 14). (Android 2.3?)
  • xUtils3变化较多所以建立了新的项目不在旧版(github.com/wyouflf/xUtils)上继续维护, 相对于旧版本:
    1. HTTP实现替换HttpClient为UrlConnection, 自动解析回调泛型, 更安全的断点续传策略.
    2. 支持标准的Cookie策略, 区分domain, path...
    3. 事件注解去除不常用的功能, 提高性能.
    4. 数据库api简化提高性能, 达到和greenDao一致的性能.
    5. 图片绑定支持gif(受系统兼容性影响, 部分gif文件只能静态显示), webp; 支持圆角, 圆形, 方形等裁剪, 支持自动旋转...

二、配置

使用Gradle构建时添加一下依赖即可:

compile 'org.xutils:xutils:3.3.40'

需要的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

初始化
 // 在application的onCreate中初始化@Overridepublic void onCreate() {super.onCreate();x.Ext.init(this);x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.}

三、View绑定

事件注解如下:

@OnClick({ R.id.btn, R.id.img })      public void clickMethod(View v) {      Toast.makeText(SecondActivity.this, "you clicked button!",      Toast.LENGTH_SHORT).show();      }      @OnItemClick(R.id.list)      public void itemClick(AdapterView<?> parent, View view, int position,long id) {      Toast.makeText(SecondActivity.this, "position--->" + position,      Toast.LENGTH_SHORT).show();      }
// xUtils的view注解要求必须提供id,以使代码混淆不受影响。
@ViewInject(R.id.textView)
TextView textView;  //@ViewInject(vale=R.id.textView, parentId=R.id.parentView)
//TextView textView;  @ResInject(id = R.string.label, type = ResType.String)
private String label;  // 取消了之前使用方法名绑定事件的方式,使用id绑定不受混淆影响
// 支持绑定多个id @OnClick({R.id.id1, R.id.id2, R.id.id3})
// or @OnClick(value={R.id.id1, R.id.id2, R.id.id3}, parentId={R.id.pid1, R.id.pid2, R.id.pid3})
// 更多事件支持参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
@OnClick(R.id.test_button)
public void testButtonClick(View v) { // 方法签名必须和接口中的要求一致  ...
}
...
//在Activity中注入:
@Override
public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  ViewUtils.inject(this); //注入view和事件  ...  textView.setText("some text...");  ...
}
//在Fragment中注入:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加载fragment布局  ViewUtils.inject(this, view); //注入view和事件  ...
}
//在PreferenceFragment中注入:
public void onActivityCreated(Bundle savedInstanceState) {  super.onActivityCreated(savedInstanceState);  ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件  ...
}
// 其他重载
// inject(View view);
// inject(Activity activity)
// inject(PreferenceActivity preferenceActivity)
// inject(Object handler, View view)
// inject(Object handler, Activity activity)
// inject(Object handler, PreferenceGroup preferenceGroup)
// inject(Object handler, PreferenceActivity preferenceActivity)  

四、图片加载

加载网络或本地bitmap的时候无需担心再遇到OOM的现象,管理bitmap的内存采用了LRU算法,同时也能避免列表滑动过程中发生图片错位等得现象。加载网络图片时,还可以配置运行线程的数量,缓存路径等。。。通过BitmapUtils的各种构造器,可以很方便的创建出本地缓存路径和缓存的大小,以及内存缓存的大小。
       ImageOptions   imageOptions = new ImageOptions.Builder().setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120)).setRadius(DensityUtil.dip2px(5))// 如果ImageView的大小不是定义为wrap_content, 不要crop..setCrop(true) // 很多时候设置了合适的scaleType也不需要它.// 加载中或错误图片的ScaleType//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX).setImageScaleType(ImageView.ScaleType.CENTER_CROP).setLoadingDrawableId(R.mipmap.ic_launcher).setFailureDrawableId(R.mipmap.ic_launcher).build();x.image().bind(imageView, url, imageOptions);// assets filex.image().bind(imageView, "assets://test.gif", imageOptions);// local filex.image().bind(imageView, new File("/sdcard/test.gif").toURI().toString(), imageOptions);x.image().bind(imageView, "/sdcard/test.gif", imageOptions);x.image().bind(imageView, "file:///sdcard/test.gif", imageOptions);x.image().bind(imageView, "file:/sdcard/test.gif", imageOptions);x.image().bind(imageView, url, imageOptions, new Callback.CommonCallback<Drawable>() {...});x.image().loadDrawable(url, imageOptions, new Callback.CommonCallback<Drawable>() {...});x.image().loadFile(url, imageOptions, new Callback.CommonCallback<File>() {...});

五、网络请求

1、完整版

get请求:

  RequestParams params = new RequestParams("https://www.baidu.com/");
Callback.Cancelable cancelable= x.http().get(params,new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {//Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();if (ex instanceof HttpException) { // 网络错误HttpException httpEx = (HttpException) ex;int responseCode = httpEx.getCode();String responseMsg = httpEx.getMessage();String errorResult = httpEx.getResult();// ...} else { // 其他错误// ...}Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {}});// cancelable.cancel(); // 取消请求

post请求:

        RequestParams params = new RequestParams("https://www.baidu.com/");params.addBodyParameter("key",value);Callback.Cancelable cancelable= x.http().post(params,new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {}});// cancelable.cancel(); // 取消请求

2、简洁版

get请求:
RequestParams params = new RequestParams("https://www.baidu.com/");params.setSslSocketFactory(...); // 设置sslparams.addQueryStringParameter("wd", "xUtils");x.http().get(params, new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {}});

post请求:

        RequestParams params = new RequestParams("https://www.baidu.com/");params.addBodyParameter("key",value);x.http().post(params,new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {}});

大家应该发现了,两种版本不就是一样的吗?对,区别就在于,完整版可以通过 Callback.Cancelable接口取消网络请求,而简洁版就不可以

3、带有缓存版

RequsetParams params = new RequsetParams();
// 默认缓存存活时间, 单位:毫秒.(如果服务没有返回有效的max-age或Expires)
params.setCacheMaxAge(1000 * 60);
Callback.Cancelable cancelable// 使用CacheCallback, xUtils将为该请求缓存数据.= x.http().get(params, new Callback.CacheCallback<String>() {private boolean hasError = false;private String result = null;@Overridepublic boolean onCache(String result) {// 得到缓存数据, 缓存过期后不会进入这个方法.// 如果服务端没有返回过期时间, 参考params.setCacheMaxAge(maxAge)方法.//// * 客户端会根据服务端返回的 header 中 max-age 或 expires 来确定本地缓存是否给 onCache 方法.//   如果服务端没有返回 max-age 或 expires, 那么缓存将一直保存, 除非这里自己定义了返回false的//   逻辑, 那么xUtils将请求新数据, 来覆盖它.//// * 如果信任该缓存返回 true, 将不再请求网络;//   返回 false 继续请求网络, 但会在请求头中加上ETag, Last-Modified等信息,//   如果服务端返回304, 则表示数据没有更新, 不继续加载数据.//this.result = result;return false; // true: 信任缓存数据, 不在发起网络请求; false不信任缓存数据.}@Overridepublic void onSuccess(String result) {// 注意: 如果服务返回304 或 onCache 选择了信任缓存, 这时result为null.if (result != null) {this.result = result;}}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {hasError = true;Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();if (ex instanceof HttpException) { // 网络错误HttpException httpEx = (HttpException) ex;int responseCode = httpEx.getCode();String responseMsg = httpEx.getMessage();String errorResult = httpEx.getResult();// ...} else { // 其他错误// ...}}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {if (!hasError && result != null) {// 成功获取数据Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}}
});

4、发送json

  RequestParams params = new RequestParams(url);params.setAsJsonContent(true);JSONObject object  = new JSONObject();//服务器需要传参的json对象object .put("key", value);//根据实际需求添加相应键值对object .put("key1", value1);object .put("key2", value2);params.setBodyContent(object.toString());

5、上传文件

  RequestParams params = new RequestParams("http://192.168.0.13:8080/upload");// 加到url里的参数, http://xxxx/s?wd=xUtilsparams.addQueryStringParameter("wd", "xUtils");// 添加到请求body体的参数, 只有POST, PUT, PATCH, DELETE请求支持.// params.addBodyParameter("wd", "xUtils");// 使用multipart表单上传文件params.setMultipart(true);params.addBodyParameter("file",new File("/sdcard/test.jpg"),null); // 如果文件没有扩展名, 最好设置contentType参数.try {params.addBodyParameter("file2",new FileInputStream(new File("/sdcard/test2.jpg")),"image/jpeg",// 测试中文文件名"你+& \" 好.jpg"); // InputStream参数获取不到文件名, 最好设置, 除非服务端不关心这个参数.} catch (FileNotFoundException ex) {ex.printStackTrace();}x.http().post(params, new Callback.CommonCallback<String>() {@Overridepublic void onSuccess(String result) {Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();}@Overridepublic void onCancelled(CancelledException cex) {Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();}@Overridepublic void onFinished() {}});

6、下载文件

   /*** * @param url 文件下载路径* @param path 文件下载后的存储路径*/private void downloadFile(final String url, String path) {RequestParams params = new RequestParams(url);params.setSaveFilePath(path);x.http().get(params, new Callback.ProgressCallback<File>() {@Overridepublic void onWaiting() {}@Overridepublic void onStarted() {}@Overridepublic void onLoading(long total, long current, boolean isDownloading) {//total是所下载的文件大小,current是已经下载的文件大小}@Overridepublic void onSuccess(File result) {//下载完成后的文件}@Overridepublic void onError(Throwable ex, boolean isOnCallback) {ex.printStackTrace();//下载失败回调}@Overridepublic void onCancelled(CancelledException cex) {//下载取消回调}@Overridepublic void onFinished() {}});}

六、数据库

1、初始化配置
DbManager.DaoConfig daoConfig = new DbManager.DaoConfig().setDbName("test.db")// 不设置dbDir时, 默认存储在app的私有目录..setDbDir(new File("/sdcard")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了..setDbVersion(2).setDbOpenListener(new DbManager.DbOpenListener() {@Overridepublic void onDbOpened(DbManager db) {// 开启WAL, 对写入加速提升巨大db.getDatabase().enableWriteAheadLogging();}}).setDbUpgradeListener(new DbManager.DbUpgradeListener() {@Overridepublic void onUpgrade(DbManager db, int oldVersion, int newVersion) {// TODO: ...// db.addColumn(...);// db.dropTable(...);// ...// or// db.dropDb();}});

2、增

  DbManager db = x.getDb(daoConfig);
//单个插入对象Parent parent = new Parent();try {db.save(parent);} catch (DbException ex) {ex.printStackTrace();}// 批量插入对象List<Parent> parentList = new ArrayList<Parent>();for (int i = 0; i < 1000; i++) {Parent parent = new Parent();parent.setAdmin(true);parent.setDate(new java.sql.Date(1234));parent.setTime(new Date());parent.setEmail(i + "_@qq.com");parentList.add(parent);}start = System.currentTimeMillis();try {db.save(parentList);} catch (DbException ex) {ex.printStackTrace();}

3、删

     List<Parent> parentList = new ArrayList<Parent>();Parent parent = new Parent();//挨个删try {db.delete(parent);} catch (DbException ex) {ex.printStackTrace();}//批量删try {db.delete(parentList);} catch (DbException ex) {ex.printStackTrace();}

4、改

       // test update 更新对象的数据parent.name = "hahaha123";parent.setEmail("wyouflf@gmail.com");db.update(parent);db.update(parent, "name", "email");db.update(Parent.class,WhereBuilder.b("id", "=", 1).and("isAdmin", "=", true),new KeyValue("name", "test_name"), new KeyValue("isAdmin", false));

5、查

        //根据关键词获取一定有序的集合List<Parent> parentList = new ArrayList<Parent>();try {parentList = db.selector(Parent.class).orderBy("id", true).limit(1000).findAll();} catch (DbException ex) {ex.printStackTrace();}//获取第一个。。。。Parent test = db.selector(Parent.class).where("id", "in", new int[]{1, 3, 6}).findFirst();// long count = db.selector(Parent.class).where("id", "in", new int[]{1, 3, 6}).count();// Parent test = db.selector(Parent.class).where("id", "between", new String[]{"1", "5"}).findFirst();//获取该对象的全部数据List<Child> children = db.selector(Child.class).findAll();//选择所有满足条件的对象List<Parent> list = db.selector(Parent.class).where("id", "<", 54).and("time", ">", calendar.getTime()).orderBy("id").limit(10).findAll();//按照一定排序获取全部List<DbModel> dbModels = db.selector(Parent.class).groupBy("name").select("name", "count(name) as count").findAll();

七、总结

上面都说完了,还要咋总结~~~
就一句话,xutils3,给力!!!

认真点,带你全面了解xutils3相关推荐

  1. android xutil 数据库,Android XUtils3框架的基本使用方法(二)

    上一篇Android中XUtils3框架使用方法详解(一)文章,主要介绍了XUtil3的注解模块,网络模块,图片加载模块,今天给大家带来数据库模块的讲解,现在主流的ORM框架很多,比如OrmLite, ...

  2. XUtils===XUtils3框架的基本使用方法

    转载自:http://blog.csdn.NET/a1002450926/article/details/50341173 今天给大家带来XUtils3的基本介绍,本文章的案例都是基于XUtils3的 ...

  3. android xutils json请求,Android Xutils3网络请求的封装详解及实例代码

    Xutils3网络请求的封装详解 封装了一个Xutil3的网络请求工具类,分享给大家,本人水平有限,不足之处欢迎指出. 使用前先配置xutils3: 1.gradle中添加 compile 'org. ...

  4. Idea groovy表生成实体类带注释

    Idea groovy表生成实体类带注释 1.点开datasourse,打开idea带的数据库工具,具体添加数据库连接,这里不描述. 这时点击会生成一个poji 这时生成的pojo中是不带中文注释的, ...

  5. 如何判断飞机的年限_技术流带你鉴定前风挡玻璃更换,不再使用日期判断!

    ​ 这又是一篇关于前风挡玻璃鉴定的文章,我记得在二手车鉴定微信公众号里面已经发布好几篇这样的文章了,当然每篇文章的住重点不同,今天这一篇应该是完结篇,它们在一起能组成一套玻璃更换系列专题课程: 我们回 ...

  6. 2022-2028年中国橡胶带行业市场运营格局及未来前景分析报告

    [报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了橡胶带行业相关概述.中国橡胶带行业运行环境.分析了中国橡胶带行业的现 ...

  7. Go 学习笔记(72)— Go 第三方库之 pkg/errors 带堆栈的错误处理

    包 github.com/pkg/errors 让开发人员很容易在 error 错误信息上带上堆栈信息,可以更快更准确定位错误,例如行号等信息. 如果项目代码比较复杂,且经常需要追踪 Bug,建议使用 ...

  8. Windows10自带搜索增强设置

    操作系统:Windows10 前言:Windows自带的搜索速度非常慢,因此有人借助Everything等索引软件进行搜索,能够显著提升搜索速度.但是Windows自带的搜索在进行一些设置以后也能达到 ...

  9. django自带的分页功能

    django自带的分页功能 django中自带的分页功能有缺陷,但是也是一种思路,所以在下做一个整理,方便以后使用,还有服务各位小伙伴. django视图部分的代码.(注释才是重点) from dja ...

最新文章

  1. table control中用帮助(F4)实现自动填充另一字段
  2. linux模糊查找子目录,linux怎么模糊查找一个文件
  3. 计算机主板等级指标,转载一篇主板文章,教你认识三大厂主板分级
  4. VDP文件级恢复需要在用VDP备份的机器上浏览
  5. 《软件项目管理(第二版)》期中考试试题总结/复习资料
  6. wireshark数据包分析实战 第三、四章
  7. 数学建模-3.插值算法
  8. vue监听用户点击区域
  9. 怎么就没发现华为Mate20 pro有这么多神奇功能!这波黑科技盘它!
  10. springboot 整合腾讯云短信
  11. 【Spark】(task5)SparkML基础(分类 | 聚类模型)
  12. gedit 编辑器使用教程
  13. 阿里云大数据ACP认证学习笔记
  14. java web没有APP流行_简单粗暴,详细得不要不要的 JavaWeb快速入门
  15. python binascii.b2a_hex_python标准模块介绍- binascii 二进制和ASCII转换
  16. 添加远程连接 Linux数据库与Navicat数据库建立连接
  17. cinnamon桌面_使用Cinnamon增强您的Linux桌面
  18. 计算机软件技术基础复习
  19. 第4期-通过起点中文网爬取小说
  20. 通过,aria-describedby属性,获取元素,并设置样式

热门文章

  1. python随机生成字符串_python生成随机数、随机字符串
  2. python去除中间空格只留一个_汇总初学Python的21个操作难点,看完别再去踩坑了...
  3. 无法接收 agent 发出的检测信号_BR-PST系列基桩非金属超声波检测仪的系统延时来源及测试校正方法...
  4. UDP通讯C++实现
  5. java stream 取某个字段_java8的函数式编程和stream使用心得
  6. code blocks 代码颜色_vuepress中实现代码折叠、高亮
  7. AS升级3.0,gradle plugin 升级到3.0.0遇到的问题
  8. 东芝发布15nm SG5固态硬盘 容量高达1TB
  9. 《企业大数据系统构建实战:技术、架构、实施与应用》一3.2 如何选择解决方案...
  10. 19.Oracle10g服务器管理恢复--RMAN表空间恢复(练习33)