android使用OkHttp实现下载的进度监听和断点续传
程序员文章站
2023-12-15 08:56:40
1. 导入依赖包
// retrofit, 基于okhttp,考虑到项目中经常会用到retrofit,就导入这个了。
compile 'com.square...
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(); } } }
最后是动态效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。