1.使用HttpUtils上传文件 或者 提交数据 到服务器(post方法)
RequestParams params = new RequestParams();
params.addHeader(name, value);
params.addQueryStringParameter(name, value);
// 只包含字符串参数时默认使用BodyParamsEntity,
// 类似于UrlEncodedFormEntity(application/x-www-form-urlencoded)。
params.addBodyParameter(name, value);
// 加入文件参数后默认使用MultipartEntity(multipart/form-data),
// 如需multipart/related,xUtils中提供的MultipartEntity支持设置subType为related。
// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
// 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
params.addBodyParameter(file, new File(path));
…
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST,
uploadUrl….,
params,
new RequestCallBack() {
@Override
//服务器请求的开始执行的方法
public void onStart() {
testTextView.setText(conn...);
}
@Override
//正在 上传的方法
public void onLoading(long total, long current, boolean isUploading) {
if (isUploading) {
testTextView.setText(upload: + current + / + total);
} else {
testTextView.setText(reply: + current + / + total);
}
}
@Override
public void onSuccess(ResponseInfo<string> responseInfo) {
testTextView.setText(reply: + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(error.getExceptionCode() + : + msg);
} });
使用HttpUtils下载文件:
支持断点续传,随时停止下载任务,开始任务
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip,
/sdcard/httpcomponents-client-4.2.5-src.zip,
true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack() {
@Override
public void onStart() {
testTextView.setText(conn...);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + / + total);
}
@Override
public void onSuccess(ResponseInfo<file> responseInfo) {
//泛型控制的是返回来的reslut的值,可用给返回的值设置解析的编码方式
testTextView.setText(downloaded: + responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(msg);
}
});
…
//调用cancel()方法停止下载
handler.cancel();
…
BitmapUtils 使用方法 可用使用config方法去配置显示的方法
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载网络图片
bitmapUtils.display(testImageView, http://bbs.lidroid.com/static/image/common/logo.png);
// 加载本地图片(路径以/开头, 绝对路径)
bitmapUtils.display(testImageView, /sdcard/test.jpg);
// 加载assets中的图片(路径以assets开头)
bitmapUtils.display(testImageView, assets/img/wallpaper.jpg);
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
输出日志 LogUtils
// 自动添加TAG,格式: className.methodName(L:lineNumber)
// 可设置全局的LogUtils.allowD = false,LogUtils.allowI = false…,控制是否输出log。
// 自定义log输出LogUtils.customLogger = new xxxLogger();
LogUtils.d(wyouflf);