zoukankan      html  css  js  c++  java
  • 断点续传下载

    Android网络编程之断线续传

    xUtils简介

    1. xUtils 包含了很多实用的android工具。
    2. xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响...

    目前xUtils主要有四大模块:

    1. DbUtils模块
    2. ViewUtils模块:
      • android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定;
      • 完全注解方式就可以进行UI绑定和事件绑定。
      • 无需findViewById和setClickListener等。
    3. HttpUtils模块:这是断点续传要用到的。
    4. BitmapUtils模块

    使用xUtils快速开发框架需要有以下权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    HttpUtils使用方法:

    使用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<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();
    ...
    
    
    ## xUtils断点续传的项目使用Demo:
    ### 第一步:下载下载开源xUtils
     - 在github中搜索xUtils,找到对于语言java编写的开源项目
    ### 第二步:开源xUtils源码包导入到android项目下
     - 解压下载的xUtils源码包,从里面`library`-->src-->`com`把此目录下的导入自己的android项目中
    ### 第三步:调用开源项目封装好的方法实现断点续传逻辑
    
    
    //[0]找到我们关心的控件
    ed_path = (EditText) findViewById(R.id.ed_path);
    pb= (ProgressBar) findViewById(R.id.progressBar1);
    Button btn = (Button) findViewById(R.id.btn);
    btn = (Button) findViewById(R.id.btn);
    //点击按钮实现断点续传下载的逻辑
    btn.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
    //[1]获取下载路径
    String path = ed_path.getText().toString().trim();
    //[2]创建httputils对象
    HttpUtils http = new HttpUtils();
    
    //[3]实现断点下载逻辑
    /**
      * url 下载路径
      * target 下载文件的路径
      * autoResume 是否支持断点续传 boolean类型
      * callback 回调事件 
      */
    http.download(path, "/mnt/sdcard/heihei.exe", true, new RequestCallBack<File>() {
    
    //下载成功  注意下载成功与请求成功是有区别的
    @Override
    public void onSuccess(ResponseInfo<File> responseInfo) {
    Toast.makeText(getApplicationContext(), "下载成功", 1).show();	
    }
    				
    /**
      * 重写onLoading方法
      * total参数  代表总进度
      * current    代表当前进度
      */
    @Override
    public void onLoading(long total, long current,
    boolean isUploading) {
    pb.setMax((int) total); //设置进度条的最大值
    pb.setProgress((int) current); //设置当前进度条的当前进度
    }
    				
    //下载失败的回调
    @Override
    public void onFailure(HttpException error, String msg) 	
    }
    });	
    }
    });
    

    开源项目的使用实现快速开发

  • 相关阅读:
    Python 模块 itertools
    Python 字符串的encode与decode
    python 模块 hashlib(提供多个不同的加密算法)
    暴力尝试安卓gesture.key
    hdu 1300 Pearls(DP)
    hdu 1232 畅通工程(并查集)
    hdu 1856 More is better(并查集)
    hdu 1198 Farm Irrigation(并查集)
    hdu 3635 Dragon Balls(并查集)
    hdu 3038 How Many Answers Are Wrong(并查集)
  • 原文地址:https://www.cnblogs.com/stephenhuashao/p/5603478.html
Copyright © 2011-2022 走看看