Android xUtils上传下载文件

来源:互联网 发布:淘宝客服外包靠谱吗 编辑:程序博客网 时间:2024/06/07 22:27

https://github.com/wyouflf/xUtils3

xUtils3简介

  • xUtils 包含了orm, http(s), image, view注解, 但依然很轻量级(246K), 并且特性强大, 方便扩展:
    • 稳定的基石AbsTask和统一的回调接口Callback, 任何异常, 即使你的回调方法实现有异常都会进入onError, 任何情况下onFinished总会让你知道任务结束了.
    • 基于高效稳定的orm工具, http模块得以更方便的实现cookie(支持domain, path, expiry等特性)和 缓存(支持Cache-Control, Last-Modified, ETag等特性)的支持.
    • 有了强大的http及其下载缓存的支持, image模块的实现相当的简洁, 并且支持回收被view持有, 但被Mem Cache移除的图片, 减少页面回退时的闪烁..
    • view注解模块仅仅400多行代码却灵活的支持了各种View注入和事件绑定, 包括拥有多了方法的listener的支持.

其他特性

  • 支持超大文件(超过2G)上传
  • 更全面的http请求协议支持(11种谓词)
  • 拥有更加灵活的ORM, 和greenDao一致的性能
  • 更多的事件注解支持且不受混淆影响...
  • 图片绑定支持gif(受系统兼容性影响, 部分gif文件只能静态显示), webp; 支持圆角, 圆形, 方形等裁剪, 支持自动旋转...
  • 从3.5.0开始不再包含libwebpbackport.so, 需要在Android4.2以下设备兼容webp的请使用3.4.0版本.

使用Gradle构建时添加一下依赖即可:

compile 'org.xutils:xutils:3.5.0'
如果使用eclipse可以 点击这里下载aar文件, 然后用zip解压, 取出jar文件.

上传文件:
public <T> HttpHandler<T> send(HttpRequest.HttpMethod method, String url, RequestParams params,                                   RequestCallBack<T> callBack) {        if (url == null) throw new IllegalArgumentException("url may not be null");        HttpRequest request = new HttpRequest(method, url);        return sendRequest(request, params, callBack);    }
HttpUtils httpUtils = new HttpUtils();httpUtils.configCurrentHttpCacheExpiry(1000 * 10);// 超时时间// Long配置当前Http缓存到期// 设置超时时间httpUtils.configTimeout(10 * 1000);// 连接超时 //指的是连接一个url的连接等待时间。httpUtils.configSoTimeout(10 * 1000);// 获取数据超时//// //指的是连接上一个url,获取response的返回等待时间RequestParams params = new RequestParams();params.addBodyParameter("token", "abc");params.addBodyParameter("image", file);HttpHandler httpHandler = httpUtils.send(HttpRequest.HttpMethod.POST,url, params, new RequestCallBack<String>() {//url,上传文件的地址@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {}@Overridepublic void onFailure(HttpException error, String msg) {}});

下载文件:

HttpUtils http = new HttpUtils();HttpHandler handler = http.download(path, "/sdcard/xxx.zip", true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。new RequestCallBack<File>() {@Overridepublic void onStart() {tv_info.setText("conn...");}@Overridepublic void onLoading(long total, long current,boolean isUploading) {tv_info.setText(current + "/" + total);}@Overridepublic void onSuccess(ResponseInfo<File> responseInfo) {tv_info.setText("downloaded:"+ responseInfo.result.getPath());}@Overridepublic void onFailure(HttpException error, String msg) {tv_info.setText(msg);}});






0 0
原创粉丝点击