Xutils的简单使用
Xutils的简单使用
- DbUtils模块:
- ViewUtils模块:
- HttpUtils模块:
- BitmapUtils模块:
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模块: 【Xutils的简单使用】加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;
支持加载网络图片和本地图片;
内存管理使用lru算法,更好的管理bitmap内存;
可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等…
使用xUtils快速开发框架需要有以下权限:
混淆时注意事项:
添加Android默认混淆配置${sdk.dir}/tools/proguard/proguard-android.txt
不要混淆xUtils中的注解类型,添加混淆配置:-keep class * extends java.lang.annotation.Annotation { *; }
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
//@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {@ViewInject(R.id.btn_http)
private Button buttonhttp;
@ViewInject(R.id.btn_db)
private Button buttondb;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x.view().inject(this);
}
@Event(value = https://www.it610.com/article/{R.id.btn_http,R.id.btn_db},type = View.OnClickListener.class)
private void Dianji(View v){switch (v.getId()){
case R.id.btn_http:
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
break;
case R.id.btn_db:
Intent intent1=new Intent(MainActivity.this,Main3Activity.class);
startActivity(intent1);
break;
default:
break;
}
}
}
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.google.gson.Gson;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {@ViewInject(R.id.httpbtn)
private Button button;
@ViewInject(R.id.lv)
private ListView listView;
ArrayList feet=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
x.view().inject(this);
x.http().get(new RequestParams("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1"), new Callback.CommonCallback>() {
@Override
public void onSuccess(String result) {
Gson gson=new Gson();
GsonBean gsonBean = gson.fromJson(result, GsonBean.class);
List data = https://www.it610.com/article/gsonBean.getData();
Log.d("###http", result);
for (int i=0;
i
package com.example.myapplication;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
public class Main3Activity extends AppCompatActivity {@ViewInject(R.id.bun_insert)
private Button button;
@ViewInject(R.id.bun_select)
private Button button1;
DbManager.DaoConfig daoConfig;
DbManager db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
x.view().inject(this);
daoConfig = new DbManager.DaoConfig();
daoConfig.setDbName("Wyc");
daoConfig.setDbVersion(1);
db = x.getDb(daoConfig);
}
@Event(value = https://www.it610.com/article/{R.id.bun_insert,R.id.bun_select},type = View.OnClickListener.class)
private void Dianji(View v){switch (v.getId()){
case R.id.bun_insert:
DbBean dbBean = new DbBean();
dbBean.setId(1);
dbBean.setName("王宇晨");
try {
db.save(dbBean);
} catch (DbException e) {
e.printStackTrace();
}
Toast.makeText(this,"存入数据库",Toast.LENGTH_SHORT).show();
break;
case R.id.bun_select:try {
DbBean first = db.selector(DbBean.class).findFirst();
Toast.makeText(this,first.toString(),Toast.LENGTH_SHORT).show();
} catch (DbException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
package com.example.myapplication;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.io.File;
public class Main4Activity extends AppCompatActivity {@ViewInject(R.id.btn_down)
private Button button;
Callback.Cancelable cancelable;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
x.view().inject(this);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("下载QQ");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelable.cancel();
}
});
}@Event(value = https://www.it610.com/article/R.id.btn_down, type = View.OnClickListener.class)
private void getdianji(View view) {RequestParams requestParams = new RequestParams();
requestParams.setUri("http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk");
//设置断点续传requestParams.setAutoRename(true);
requestParams.setSaveFilePath(Environment.getExternalStorageDirectory() + "/qq.apk");
requestParams.setCancelFast(true);
cancelable = x.http().get(requestParams, new Callback.ProgressCallback() {
@Override
public void onSuccess(File result) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}@Override
public void onError(Throwable ex, boolean isOnCallback) {}@Override
public void onCancelled(CancelledException cex) {}@Override
public void onFinished() {
progressDialog.cancel();
}@Override
public void onWaiting() {}@Override
public void onStarted() {
progressDialog.show();
}@Override
public void onLoading(long total, long current, boolean isDownloading) {progressDialog.setProgress((int) (current * 100 / total));
}
});
}
}
package com.example.myapplication;
import android.app.Application;
import org.xutils.x;
public class XmyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(true);
}
}
package com.example.myapplication;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ListBaseAdapter extends BaseAdapter {ArrayList feet;
Context context;
public ListBaseAdapter(ArrayList feet, Context context) {
this.feet = feet;
this.context = context;
}@Override
public int getCount() {
return feet.size();
}@Override
public Object getItem(int position) {
return feet.get(position);
}@Override
public long getItemId(int position) {
return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=View.inflate(context,R.layout.listview_item,null);
viewHolder.textView=convertView.findViewById(R.id.item_item);
viewHolder.imageView=convertView.findViewById(R.id.item_image);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(feet.get(position).name);
Picasso.with(context).load(feet.get(position).imageurl).into(viewHolder.imageView);
return convertView;
}class ViewHolder{
TextView textView;
ImageView imageView;
}
}
package com.example.myapplication;
public class Foot {String name;
String imageurl;
public Foot(String name, String imageurl) {
this.name = name;
this.imageurl = imageurl;
}
}
package com.example.myapplication;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
@Table(name = "school")
public class DbBean {@Column(name = "id",isId = true)
private int id;
@Column(name = "name")
private String 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 "DbBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
文章图片
文章图片
文章图片
文章图片
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量