xUtils是基于Afinal开发的目前功能比较完善的一个android开源框架
总的说,xUtils主要有四个模块:注解模块、网络模块、图片加载模块、数据库模块
implementation 'org.xutils:xutils:3.5.1'
public class XUtilsApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG); //这个会影响性能
}
}
注解(Annotation)为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(MainActivity.this);
}
}
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@ViewInject(R.id.button)
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(MainActivity.this);
}
}
@Event(value={R.id.button},type=View.OnClickListener.class)
private void buttonClickEvent(View view){
//TODO 单击事件,value里面存放的是需要单击的控件集合,type为控件的事件类型
//参数和控件的单击事件参数一致
}
xUtils3网络模块大大方便了在实际开发中网络模块的开发,xUtils网络模块大致包括GET请求、POST请求、如何使用其他的请求方式、上传方式、下载方式、使用缓存等功能
public static void get(String Url, Map map, final BackCallNet backCallNet){
RequestParams requestParams = new RequestParams(Url);
for (String key:map.keySet()){
requestParams.addParameter(key,map.get(key));
}
//CommomCallback是通用的一个回调接口,参数String使我们需要返回的值
x.http().get(requestParams, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
backCallNet.successful(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
ex.getMessage();
backCallNet.failed(ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public interface BackCallNet{
void successful(String stream);
void failed(Throwable ex);
}
public static void post(String url, Map map, final BackCallNet backCallNet){
RequestParams requestParams = new RequestParams(url);
for (String key:map.keySet()){
requestParams.addParameter(key,map.get(key));
}
x.http().post(requestParams, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
backCallNet.successful(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
backCallNet.failed(ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public interface BackCallNet{
void successful(String stream);
void failed(Throwable ex);
}
//第一次采用网络的方式进行访问,在缓存的时间内去获取缓存的结果,这里设置的10s,
//10s结束之后会在此访问网络。
//在缓存期间,先访问onCache方法,再访问onSuccess方法。访问onSuccess方法的时候,参数的值是空值,因为没有访问网络
public static void cacheGet(String url, Map map, final BackCallNet backCallNet){
RequestParams requestParams = new RequestParams(url);
requestParams.setCacheMaxAge(10*1000); //添加缓存的时间
x.http().get(requestParams, new Callback.CacheCallback() {
@Override
public void onSuccess(String result) {
Log.d("stcLog","access network>>" + (TextUtils.isEmpty(result)));
if (result != null) {
backCallNet.successful(result);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
backCallNet.failed(ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public boolean onCache(String result) {
Log.d("stcLog","get cache result");
//TODO 在setCacheMaxAge设置范围,如果再次调用Get请求
//TODO 返回true:缓存内容返回,相信本地缓存。
//TODO 返回false,缓存内容被返回,不相信本地缓存,任然请求网络
backCallNet.cache("缓存" + result);
return true;
}
});
}
public interface BackCallNet{
void successful(String stream);
void failed(Throwable ex);
void cache(String stream);
}
public static void cachePost(String url, Map map, final BackCallNet backCallNet){
RequestParams requestParams = new RequestParams(url);
requestParams.setCacheMaxAge(10*1000);
x.http().post(requestParams, new Callback.CacheCallback() {
@Override
public void onSuccess(String result) {
//在缓存期间,会调用这个方法,但是result为空
if (result != null) {
backCallNet.successful(result);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
backCallNet.failed(ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public boolean onCache(String result) {
backCallNet.cache(result);
return true;
}
});
}
public interface BackCallNet{
void successful(String stream);
void failed(Throwable ex);
void cache(String stream);
}
public static void uploadFile(String url, final BackCallNet backCallNet){
String path = Environment.getExternalStorageDirectory().getPath();
File file = new File(path + "/Document/test");
final RequestParams requestParams = new RequestParams(url);
requestParams.setMultipart(true);
requestParams.addBodyParameter("file",file);
x.http().get(requestParams, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
if (result != null) {
backCallNet.successful(result);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
backCallNet.failed(ex);
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public static void downFile(final Context context, String url, BackCallNet backCallNet){
String path = Environment.getExternalStorageDirectory().getPath();
final File file = new File(path + "/Document/qq");
RequestParams requestParams = new RequestParams(url);
//下载的文件保存地址
requestParams.setSaveFilePath(file.getPath());
//自动为文件命名
requestParams.setAutoRename(true);
x.http().post(requestParams, new Callback.ProgressCallback() {
@Override
public void onSuccess(File result) {
Log.d("stcLog","下载成功:" + result);
//这是在调用安装程序
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result),"application/vnd.android.package-archive");
context.startActivity(intent);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.d("stcLog","下载失败:" + ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
Log.d("stcLog","成功");
}
//网络请求之前回调
@Override
public void onWaiting() {
Log.d("stcLog","准备开始下载");
}
//网络请求开始的时候回调
@Override
public void onStarted() {
Log.d("stcLog","开始下载");
}
//下载的时候不间断的回调
@Override
public void onLoading(long total, long current, boolean isDownloading) {
Log.d("stcLog","总大小:" + getTwo(total) + "M,已下载大小:" + getTwo(current) + "M" + ",进度:" + getProgress(total,current) + "%");
}
});
}
xUtils图片模块,重点在于加载图片的4个bind方法,loadDrawable与loadFile和imageOptions用法
bind的几个方法
//没有imageOptions普通加载
public static void loadCommonImage(ImageView imageView, String url){
x.image().bind(imageView,url);
}
//有ImageOptions的加载圆形图片
public static void loadImageOptionsImage(ImageView imageView,String url){
ImageOptions imageOptions = new ImageOptions.Builder().setCircular(true).build();
x.image().bind(imageView,url,imageOptions);
}
//有ImageOptions的加载圆角
public static void loadImageOptionsImage(ImageView imageView,String url){
ImageOptions imageOptions = new ImageOptions.Builder().setRadius(200).setFadeIn(true).build();
x.image().bind(imageView,url,imageOptions);
}
public static void loadCommonCalbackImage(ImageView imageView,String url){
x.image().bind(imageView, url, new Callback.CommonCallback() {
@Override
public void onSuccess(Drawable result) {
Log.d("stcLog","[loadCommonCalbackImage onSuccess]:" + result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public static void loadDrawable(final ImageView imageView, String url){
x.image().loadDrawable(url, null, new Callback.CommonCallback() {
@Override
public void onSuccess(Drawable result) {
imageView.setImageDrawable(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public static void loadFile(final ImageView imageView, String url){
x.image().loadFile(url, null, new Callback.CacheCallback() {
@Override
public boolean onCache(File result) {
//这里做图片的另存操作
return false;
}
@Override
public void onSuccess(File result) {
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public static void initDB(){
DbManager.DaoConfig mDbManager = new DbManager.DaoConfig();
//设置数据库的名字,默认是xutils.db
mDbManager.setDbName("sun.db");
//设置数据库允许事务
mDbManager.setAllowTransaction(true);
//设置数据库的版本
mDbManager.setDbVersion(1);
//设置表创建监听
mDbManager.setTableCreateListener(new DbManager.TableCreateListener() {
@Override
public void onTableCreated(DbManager db, TableEntity> table) {
Log.d("stcLog","create table listener:" + table.getName());
}
});
//设置数据库更新的监听
mDbManager.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
}
});
//设置数据库打开的监听
mDbManager.setDbOpenListener(new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
Log.d("stcLog","database open listener");
//开启数据库支持多线程操作,提高性能
db.getDatabase().enableWriteAheadLogging();
}
});
DbManager db = x.getDb(mDbManager);
}
/**
* onCreated = "sql" sql:当第一次创建表需要插入数据时候在此写sql语句例:CREATE UNIQUE INDEX index_name ON person(id,name)
*/
//创建数据库的实体表
@Table(name="person",onCreated="")
public class Person {
//name数据库的一个字段,isId数据库的主键,autoGen是否自动增长,property添加约束
@Column(name="id",isId=true,autoGen=true,property="NOT NULL")
private int id;
@Column(name="name")
private String name;
//必须有一个空的构造函数,否则创建失败
public Person() {
}
public Person(String name) {
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
DbManager.DaoConfig daoConfig = dbClass.initDB();
db = x.getDb(daoConfig);
List list = new ArrayList<>();
Person person_zhang = new Person("张三");
Person person_li = new Person("李四");
Person person_wang = new Person("王五");
list.add(person_zhang);
list.add(person_li);
list.add(person_wang);
try {
//不仅可以插入单条数据,也可以插入集合
db.save(list);
} catch (DbException e) {
e.printStackTrace();
}
try {
db.dropDb();
} catch (DbException e) {
e.printStackTrace();
}
try {
db.dropTable(Person.class);
} catch (DbException e) {
e.printStackTrace();
}
//查询第一个数据
if (db != null) {
try {
Person first = db.findFirst(Person.class);
displayContent.setText(first.toString());
} catch (DbException e) {
e.printStackTrace();
}
}else {
Toast.makeText(this,"数据库为空",Toast.LENGTH_SHORT).show();
}
//查询所有的数据
if (db != null) {
try {
List all = db.findAll(Person.class);
displayContent.setText("");
for (int i=0;i",1);
try {
List all = db.selector(Person.class).where(whereBuilder).findAll();
displayContent.setText("");
for (int i=0;i",1).findAll();
} catch (DbException e) {
e.printStackTrace();
}
}else {
Toast.makeText(this,"数据库为空",Toast.LENGTH_SHORT).show();
}
//第一种
try {
Person first = db.findFirst(Person.class);
first.setName("张三01");
db.update(first,"name");
} catch (DbException e) {
e.printStackTrace();
}
//第二种
try {
Person first = db.findFirst(Person.class);
first.setName("张三02");
db.saveOrUpdate(first);
} catch (DbException e) {
e.printStackTrace();
}
//第一种(删除表里面的所有数据)
try {
db.delete(Person.class);
} catch (DbException e) {
e.printStackTrace();
}
//第二种
WhereBuilder whereBuilder = WhereBuilder.b();
whereBuilder.and("id","=","2");
try {
db.delete(Person.class,whereBuilder);
} catch (DbException e) {
e.printStackTrace();
}