博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络下载-xUtils,HttpUtils
阅读量:4290 次
发布时间:2019-05-27

本文共 2866 字,大约阅读时间需要 9 分钟。

xUtils有四大模块:
数据库DbUtils,控件ViewUtils,网络ViewUtils,图片BitmapUtils
// get 提交
HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.GET,    "http://www.lidroid.com",    new RequestCallBack
(){ @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo
responseInfo) { textView.setText(responseInfo.result); } @Override public void onStart() { } @Override public void onFailure(HttpException error, String msg) { }});
// 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<String>() {
        @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 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<File>() {
        @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) {
            testTextView.setText("downloaded:" + responseInfo.result.getPath());
        }
        @Override
        public void onFailure(HttpException error, String msg) {
            testTextView.setText(msg);
        }
});
...
//调用cancel()方法停止下载
handler.cancel();
...

转载地址:http://sbegi.baihongyu.com/

你可能感兴趣的文章
简单MySQL教程二
查看>>
mysql学习之 explain
查看>>
java搜索引擎Apache的solr初探-安装使用导入mysql数据
查看>>
Java互联网架构-企业级实战秒杀系统优化方案与应用思路
查看>>
简单介绍一下Spring / java中Spring框架7大核心模块的作用,如何在面试中侃侃而谈?/ Spring体系常用项目一览
查看>>
为什么使用mq?
查看>>
配置使用IM表达式的基本任务
查看>>
自定义JSP标签自动完成对页面按钮做权限拦截处理
查看>>
Java互联网架构-负载均衡原理与实现方案
查看>>
实用SQL函数集合(五)《格式化函数》
查看>>
jvm-运行时内存结构
查看>>
Java开发秒杀大型互联网企业高并发限流特技
查看>>
Eclipse+Maven+Spring+CXF 构建webservice 服务
查看>>
Java Collections.addAll() 与 ArrayList.addAll() 的区别
查看>>
消息中间件 kafka+zookeeper 集群部署、测试与应用(1)
查看>>
Eclipse 自定义本地maven仓库位置
查看>>
centos7-codis安装部署,解决redis分布式的方案
查看>>
java基于redis客户端redisson的RPC远程服务调用
查看>>
JVM内存、堆模型、垃圾回收器总结
查看>>
sql(join中on与where区别) / NVL函数 / oracle存储过程中is和as区别 / JAVA调用数据库存储过程
查看>>