android使用OkHttp实现下载的进度监听和断点续传

作者:梦想拒绝零风险 时间:2022-03-23 12:37:07 

1. 导入依赖包


// retrofit, 基于Okhttp,考虑到项目中经常会用到retrofit,就导入这个了。
 compile 'com.squareup.retrofit2:retrofit:2.1.0'
// ButterKnife
 compile 'com.jakewharton:butterknife:7.0.1'
// rxjava 本例中线程切换要用到,代替handler
 compile 'io.reactivex:rxjava:1.1.6'
 compile 'io.reactivex:rxandroid:1.2.1'

2. 继承ResponseBody,生成带进度监听的ProgressResponseBody


// 参考okhttp的官方demo,此类当中我们主要把注意力放在ProgressListener和read方法中。在这里获取文件总长我写在了构造方法里,这样免得在source的read方法中重复调用或判断。读者也可以根据个人需要定制自己的 * 。
public class ProgressResponseBody extends ResponseBody {

public interface ProgressListener {
   void onPreExecute(long contentLength);
   void update(long totalBytes, boolean done);
 }

private final ResponseBody responseBody;
 private final ProgressListener progressListener;
 private BufferedSource bufferedSource;

public ProgressResponseBody(ResponseBody responseBody,
               ProgressListener progressListener) {
   this.responseBody = responseBody;
   this.progressListener = progressListener;
   if(progressListener!=null){
     progressListener.onPreExecute(contentLength());
   }
 }

@Override
 public MediaType contentType() {
   return responseBody.contentType();
 }

@Override
 public long contentLength() {
   return responseBody.contentLength();
 }

@Override
 public BufferedSource source() {
   if (bufferedSource == null) {
     bufferedSource = Okio.buffer(source(responseBody.source()));
   }
   return bufferedSource;
 }

private Source source(Source source) {
   return new ForwardingSource(source) {
     long totalBytes = 0L;
     @Override
     public long read(Buffer sink, long byteCount) throws IOException {
       long bytesRead = super.read(sink, byteCount);
       // read() returns the number of bytes read, or -1 if this source is exhausted.
       totalBytes += bytesRead != -1 ? bytesRead : 0;
       if (null != progressListener) {
         progressListener.update(totalBytes, bytesRead == -1);
       }
       return bytesRead;
     }
   };
 }
}

3.创建ProgressDownloader


//带进度监听功能的辅助类
public class ProgressDownloader {

public static final String TAG = "ProgressDownloader";

private ProgressListener progressListener;
 private String url;
 private OkHttpClient client;
 private File destination;
 private Call call;

public ProgressDownloader(String url, File destination, ProgressListener progressListener) {
   this.url = url;
   this.destination = destination;
   this.progressListener = progressListener;
   //在下载、暂停后的继续下载中可复用同一个client对象
   client = getProgressClient();
 }
 //每次下载需要新建新的Call对象
 private Call newCall(long startPoints) {
   Request request = new Request.Builder()
       .url(url)
       .header("RANGE", "bytes=" + startPoints + "-")//断点续传要用到的,指示下载的区间
       .build();
   return client.newCall(request);
 }

public OkHttpClient getProgressClient() {
 // * ,用上ProgressResponseBody
   Interceptor interceptor = new Interceptor() {
     @Override
     public Response intercept(Chain chain) throws IOException {
       Response originalResponse = chain.proceed(chain.request());
       return originalResponse.newBuilder()
           .body(new ProgressResponseBody(originalResponse.body(), progressListener))
           .build();
     }
   };

return new OkHttpClient.Builder()
       .addNetworkInterceptor(interceptor)
       .build();
 }

// startsPoint指定开始下载的点
 public void download(final long startsPoint) {
   call = newCall(startsPoint);
   call.enqueue(new Callback() {
         @Override
         public void onFailure(Call call, IOException e) {

}

@Override
         public void onResponse(Call call, Response response) throws IOException {
           save(response, startsPoint);
         }
       });
 }

public void pause() {
   if(call!=null){
     call.cancel();
   }
 }

private void save(Response response, long startsPoint) {
   ResponseBody body = response.body();
   InputStream in = body.byteStream();
   FileChannel channelOut = null;
   // 随机访问文件,可以指定断点续传的起始位置
   RandomAccessFile randomAccessFile = null;
   try {
     randomAccessFile = new RandomAccessFile(destination, "rwd");
     //Chanel NIO中的用法,由于RandomAccessFile没有使用缓存策略,直接使用会使得下载速度变慢,亲测缓存下载3.3秒的文件,用普通的RandomAccessFile需要20多秒。
     channelOut = randomAccessFile.getChannel();
     // 内存映射,直接使用RandomAccessFile,是用其seek方法指定下载的起始位置,使用缓存下载,在这里指定下载位置。
     MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, startsPoint, body.contentLength());
     byte[] buffer = new byte[1024];
     int len;
     while ((len = in.read(buffer)) != -1) {
       mappedBuffer.put(buffer, 0, len);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }finally {
     try {
       in.close();
       if (channelOut != null) {
         channelOut.close();
       }
       if (randomAccessFile != null) {
         randomAccessFile.close();
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
}

4. 测试demo

清单文件中添加网络权限和文件访问权限


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

MainActivity


public class MainActivity extends AppCompatActivity implements ProgressResponseBody.ProgressListener {

public static final String TAG = "MainActivity";
 public static final String PACKAGE_URL = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/weixin_821.apk";
 @Bind(R.id.progressBar)
 ProgressBar progressBar;
 private long breakPoints;
 private ProgressDownloader downloader;
 private File file;
 private long totalBytes;
 private long contentLength;

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   ButterKnife.bind(this);
 }

@OnClick({R.id.downloadButton, R.id.cancel_button, R.id.continue_button})
 public void onClick(View view) {
   switch (view.getId()) {
     case R.id.downloadButton:
     // 新下载前清空断点信息
       breakPoints = 0L;
       file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sample.apk");
       downloader = new ProgressDownloader(PACKAGE_URL, file, this);
       downloader.download(0L);
       break;
     case R.id.pause_button:
       downloader.pause();
       Toast.makeText(this, "下载暂停", Toast.LENGTH_SHORT).show();
       // 存储此时的totalBytes,即断点位置。
       breakPoints = totalBytes;
       break;
     case R.id.continue_button:
       downloader.download(breakPoints);
       break;
   }
 }

@Override
 public void onPreExecute(long contentLength) {
   // 文件总长只需记录一次,要注意断点续传后的contentLength只是剩余部分的长度
   if (this.contentLength == 0L) {
     this.contentLength = contentLength;
     progressBar.setMax((int) (contentLength / 1024));
   }
 }

@Override
 public void update(long totalBytes, boolean done) {
   // 注意加上断点的长度
   this.totalBytes = totalBytes + breakPoints;
   progressBar.setProgress((int) (totalBytes + breakPoints) / 1024);
   if (done) {
   // 切换到主线程
     Observable
         .empty()
         .observeOn(AndroidSchedulers.mainThread())
         .doOnCompleted(new Action0() {
           @Override
           public void call() {
             Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
           }
         })
         .subscribe();
   }
 }
}

最后是动态效果图

android使用OkHttp实现下载的进度监听和断点续传

来源:http://blog.csdn.net/kevinscsdn/article/details/51934274

标签:okhttp,android
0
投稿

猜你喜欢

  • 分享Java常用开发编辑器工具

    2023-11-06 07:35:37
  • 如何通过指针突破C++类的访问权限

    2022-01-26 14:14:58
  • C++高并发内存池的整体设计和实现思路

    2023-07-03 16:29:31
  • Java一个简单的红包生成算法

    2023-12-12 10:56:50
  • Springboot webscoket自定义定时器

    2023-02-12 05:34:02
  • Mybatis分页插件PageHelper的配置和简单使用方法(推荐)

    2022-03-20 06:34:55
  • SpringBoot+easypoi实现数据的Excel导出

    2023-04-05 12:27:19
  • 详解Http请求中Content-Type讲解以及在Spring MVC中的应用

    2022-10-19 04:14:11
  • 使用Android studio3.6的java api方式调用opencv

    2023-10-10 17:16:38
  • 用代码更新你的jar包

    2023-09-19 23:01:04
  • SpringBoot文件上传大小设置方式(yml中配置)

    2021-09-21 09:26:47
  • SpringBoot整合rockerMQ消息队列详解

    2021-10-03 10:55:14
  • C#多线程系列之资源池限制

    2022-01-02 17:11:04
  • C# yield关键字详解

    2021-08-01 13:06:06
  • 通过与Java功能上的对比来学习Go语言

    2023-02-18 02:04:53
  • android实现打地鼠游戏

    2023-09-25 08:45:59
  • 轻松实现Android仿淘宝地区选择功能

    2022-02-25 03:01:13
  • SpringCloud网关组件zuul实例解析

    2023-04-13 08:51:47
  • Java编程实现从尾到头打印链表代码实例

    2021-12-28 12:13:41
  • Android实现qq列表式的分类悬浮提示

    2022-09-01 09:23:44
  • asp之家 软件编程 m.aspxhome.com