Androidx学习笔记(47)--- 借助xUtils实现下载
来源:互联网 发布:淘宝商品详情图做法 编辑:程序博客网 时间:2024/05/23 21:13
HttpUtils本身就支持多线程断点续传,使用起来非常的方便
创建HttpUtils对象
HttpUtils http = new HttpUtils();
下载文件
http.download(url, //下载请求的网址 target, //下载的数据保存路径和文件名 true, //是否开启断点续传 true, //如果服务器响应头中包含了文件名,那么下载完毕后自动重命名 new RequestCallBack<File>() {//侦听下载状态 //下载成功此方法调用 @Override public void onSuccess(ResponseInfo<File> arg0) { tv.setText("下载成功" + arg0.result.getPath()); } //下载失败此方法调用,比如文件已经下载、没有网络权限、文件访问不到,方法传入一个字符串参数告知失败原因 @Override public void onFailure(HttpException arg0, String arg1) { tv.setText("下载失败" + arg1); } //在下载过程中不断的调用,用于刷新进度条 @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); //设置进度条总长度 pb.setMax((int) total); //设置进度条当前进度 pb.setProgress((int) current); tv_progress.setText(current * 100 / total + "%"); }});
代码
public class MainActivity extends Activity {private TextView tv_failure;private TextView tv_progress;private ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_failure = (TextView) findViewById(R.id.tv_failure);tv_progress = (TextView) findViewById(R.id.tv_progress);pb = (ProgressBar) findViewById(R.id.pb);}public void click(View v){HttpUtils utils = new HttpUtils();String fileName = "QQPlayer.exe";//确定下载地址String path = "http://192.168.13.13:8080/" + fileName;utils.download(path, //下载地址"sdcard/QQPlayer.exe", //文件保存路径true,//是否支持断点续传true, new RequestCallBack<File>() {//下载成功后调用@Overridepublic void onSuccess(ResponseInfo<File> arg0) {Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();}//下载失败调用@Overridepublic void onFailure(HttpException arg0, String arg1) {// TODO Auto-generated method stubtv_failure.setText(arg1);}//显示进度的时候调用@Overridepublic void onLoading(long total, long current,boolean isUploading) {// TODO Auto-generated method stubsuper.onLoading(total, current, isUploading);pb.setMax((int)total);pb.setProgress((int)current);tv_progress.setText(current * 100 / total + "%");}});}}
0 0
- Androidx学习笔记(47)--- 借助xUtils实现下载
- 借助Xutils实现多线程下载
- Androidx学习笔记(42)--- 多线程下载(java项目)
- Androidx学习笔记(46)--- 用进度条显示下载进度
- Androidx学习笔记(9)-- logcat
- Androidx学习笔记(18)-- junit单元测试
- Androidx学习笔记(19)-- SQLite 简介
- Androidx学习笔记(21)-- SQLite 事务
- Androidx学习笔记(24)-- ArrayAdapter&SimpleAdapter
- Androidx学习笔记(24)--对话框
- Androidx学习笔记(48)--- 创建Activity
- Androidx学习笔记(51)--- Activity生命周期
- Androidx学习笔记(55)--- 广播接收者
- Androidx学习笔记(62)--- 服务Service
- Androidx学习笔记(68)--- 服务分类
- Androidx学习笔记(69)--- AIDL
- Androidx学习笔记(70)--- 进程优先级
- Androidx学习笔记(75)--- 画画板
- 计数排序——CountingSort
- Androidx学习笔记(46)--- 用进度条显示下载进度
- LeetCode29. Divide Two Integers一个好答案
- lightoj1150 - Ghosts!
- 挖Linux中的古老缩略语 etc rc bin tty
- Androidx学习笔记(47)--- 借助xUtils实现下载
- Cocos2d-x TextAtlas的使用
- Webpack打包React报错Unexcepted token <
- 【slighttpd】基于lighttpd架构的Server项目实战(2)—预备知识之libevent
- Androidx学习笔记(48)--- 创建Activity
- centos 中打字的小键盘不见了
- 【慕课笔记】U3 继承 第2节 JAVA中的方法重写
- JAVA WEB学习路线
- UITableViewCell的循环利用 - 在storyboard里