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

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

概述1.导入依赖包//retrofit,基于Okhttp,考虑到项目中经常会用到retrofit,就导入这个了。

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,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实现下载的进度监听和断点续传全部内容,希望文章能够帮你解决android使用OkHttp实现下载的进度监听和断点续传所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存