XUtils的简单使用

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
						
					}
				});
	}



3 BitmapUtils

	/**
	 * 从网络上面获取图片
	 */
	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);

}



菜鸟的成长之路,因为有你所以精彩


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值