Android:XUtils框架的简单应用
XUtils是目前git上比较活跃 功能比较完善的一个框架,是基于afinal开发的,比afinal稳定性提高了很多。
xUtils简介
- xUtils 包含了很多实用的android工具。
- xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响...
- xUitls最低兼容android 2.2 (api level 8)
- 目前xUtils主要有四大模块:
-
DbUtils模块:
- android中的orm框架,一行代码就可以进行增删改查;
- 支持事务,默认关闭;
- 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);
- 支持绑定外键,保存实体时外键关联实体自动保存或更新;
- 自动加载外键关联实体,支持延时加载;
- 支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。
- ViewUtils模块:
- android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定;
- 新的事件绑定方式,使用混淆工具混淆后仍可正常工作;
- 目前支持常用的20种事件绑定,参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
- HttpUtils模块:
- 支持同步,异步方式的请求;
- 支持大文件上传,上传大文件不会oom;
- 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求;
- 下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
- 返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。
- BitmapUtils模块:
- 加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;
- 支持加载网络图片和本地图片;
- 内存管理使用lru算法,更好的管理bitmap内存;
- 可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等...
-
使用xUtils快速开发框架需要有以下权限:
<uses-permission android:name="android.permission.INTERNET" /> 访问网络权限
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> sd卡写入权限
- 今天主要了解一下HttpUtils的简单使用
- activity:
public class MainActivity extends Activity {
TextView tv;
TextView tvprogress;
ProgressBar pb;
// 下载的文件的地址
String urlpath = "http://192.168.0.101:8080/epp.exe";@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
pb = (ProgressBar) findViewById(R.id.pb);
tvprogress = (TextView) findViewById(R.id.tv_Progress);
}// 下载方法
public void click(View v) {
HttpUtils utils = new HttpUtils();
utils.download(urlpath // 文件下载地址
, "sdcard/epp.exe" // 下载后文件的位置
, true // 是否续传
, true,//
new RequestCallBack<File>() {// 下载成功
@Override
public void onSuccess(ResponseInfo<File> arg0) {
// Auto-generated method stub
Toast.makeText(MainActivity.this,
arg0.result.getPath(), 0).show();
}// 下载失败
@Override
public void onFailure(HttpException arg0, String arg1) {
// Auto-generated method stub
tv.setText(arg1);
}@Override
public void onLoading(long total, long current,
boolean isUploading) {
// Auto-generated method stub
super.onLoading(total, current, isUploading);
pb.setMax((int) total);
pb.setProgress((int) current);
tvprogress.setText(current * 100 / total + "%");
}
});
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
} - xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lession.xutilsDownLoad.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始下载"
android:onClick="click" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /><ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/tv_Progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
浙公网安备 33010602011771号