Httputils专题

来源:互联网 发布:新剑侠情缘mac下载 编辑:程序博客网 时间:2024/05/18 00:48

xUtils是用的比较多的开源组件,使用xUtils可以方便的实现取得网络数据、下载、增删改查数据和加载本地及网络图片等;

目前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" />

httpUtils的使用:

1.普通的get方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.main_tv);
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
hu = new HttpUtils();
hu.send(HttpMethod.GET, "http://www.baidu.com", callBack);
}
private RequestCallBack<String> callBack = new RequestCallBack<String>() {

@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String result = arg0.result;
pd.dismiss();
tv.setText(result);
}


@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast to = Toast.makeText(getApplicationContext(), "解析失败", Toast.LENGTH_LONG);
to.setGravity(Gravity.CENTER, 0, 0);
to.show();

}
public void onLoading(long total, long current, boolean isUploading) {
pd.setProgress((int) ((current*100)/total));
};
};

这个可以用来对于聚合数据 的接口进行get数据 从而完成数据的解析!

2.使用HttpUtils下载文件;

支持断点续传,随时停止下载任务、开始任务

public void down_onclick(View v)
{
pd.show();
hu.download("http://down.sandai.net/thunderspeed/ThunderSpeed1.0.34.360.exe", 
"/mnt/sdcard/music/my滑板鞋.mp3", callback);
}
private RequestCallBack<File> callback = new RequestCallBack<File>() {


@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub

}


@Override
public void onSuccess(ResponseInfo<File> arg0) {
// TODO Auto-generated method stub
pd.dismiss();
File file = arg0.result;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+file.getAbsolutePath()), "audio/mp3");
startActivity(intent);
}
public void onLoading(long total, long current, boolean isUploading) {
pd.setProgress((int)(current*100/total));
};

};


使用HttpUtils上传文件 或者 提交数据 到服务器(post方法)


public void post_click(View v)
{
HttpUtils hu = new HttpUtils();

//发post请求
RequestParams params  = new RequestParams();
params.addBodyParameter("action", "GetAllNames");

hu.send(HttpMethod.GET, "http://wwwtest.rupeng.cn:8888/QueryAvatarInfo.ashx", params, callBack);

}

private RequestCallBack<String> callBack = 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
String str = arg0.result;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}

};

具体看xUtills于github的详细介绍: https://github.com/wyouflf/xUtils

上传图片
0 0
原创粉丝点击