XUtils是一个第三方的组件,分为4个模块
1 ViewUtils 用于绑定ID
2 HttpUtils 用于请求网络数据
3 BitmapUtils 用于请求网络图片
4 DBUtils 支持数据库的OOP访问
1 ViewUtils
@ViewInject(R.id.xutils)
// 就相当于之前的 findViewById(id), 注意一定要启用ViewUtils的功能
private TextView xutils;
@ViewInject(R.id.img)
private ImageView img
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xutils_main);
ViewUtils.inject(this);// 启用ViewUtils功能
2 HttpUtils
/**
* 从网络上面解析数据
*/
public void getHttp() {
HttpUtils http = new HttpUtils();
// method代表请求类型, url代表网址, callBack代表请求的回调函数
http.send(HttpMethod.GET, "http://tr.api.gson.cn/system/feedback/3",
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// 请求成功回调函数
String URL = arg0.result;
JSONObject obj = JSON.parseObject(URL);
JSONArray Array = obj.getJSONArray("data");
JSONObject index = Array.getJSONObject(0);
String content = index.getString("id");
xutils.setText(content);
}
});
}
/**
* 从本地传递到网络
*/
public void postHttp() {
HttpUtils http = new HttpUtils();
RequestParams params = new RequestParams();
params.addBodyParameter("content", "你好啊");
// method代表请求类型, url网络地址, params代表传递的参数, callBack代表请求回调的函数
http.send(HttpMethod.POST, "http://tr.api.gson.cn/system/feedback/3",
params, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* 从网络上面获取图片
*/
public void Bitmap() {
BitmapUtils bitmap = new BitmapUtils(this);
bitmap.display(img, url);
}
4 DBUtils
public void update(Account account) throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "update account set name=?,money=? where id=?";
Object params[] = {account.getName(),account.getMoney(),account.getId()};
//使用service层传递过来的Connection对象操作数据库
qr.update(conn,sql, params);
}