android使用OkHttp實(shí)現(xiàn)下載的進(jìn)度監(jiān)聽和斷點(diǎn)續(xù)傳
1. 導(dǎo)入依賴包
// retrofit, 基于Okhttp,考慮到項(xiàng)目中經(jīng)常會(huì)用到retrofit,就導(dǎo)入這個(gè)了。 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,生成帶進(jìn)度監(jiān)聽的ProgressResponseBody
// 參考o(jì)khttp的官方demo,此類當(dāng)中我們主要把注意力放在ProgressListener和read方法中。在這里獲取文件總長(zhǎng)我寫在了構(gòu)造方法里,這樣免得在source的read方法中重復(fù)調(diào)用或判斷。讀者也可以根據(jù)個(gè)人需要定制自己的監(jiān)聽器。
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.創(chuàng)建ProgressDownloader
//帶進(jìn)度監(jiān)聽功能的輔助類
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;
//在下載、暫停后的繼續(xù)下載中可復(fù)用同一個(gè)client對(duì)象
client = getProgressClient();
}
//每次下載需要新建新的Call對(duì)象
private Call newCall(long startPoints) {
Request request = new Request.Builder()
.url(url)
.header("RANGE", "bytes=" + startPoints + "-")//斷點(diǎn)續(xù)傳要用到的,指示下載的區(qū)間
.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指定開始下載的點(diǎn)
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;
// 隨機(jī)訪問文件,可以指定斷點(diǎn)續(xù)傳的起始位置
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(destination, "rwd");
//Chanel NIO中的用法,由于RandomAccessFile沒有使用緩存策略,直接使用會(huì)使得下載速度變慢,親測(cè)緩存下載3.3秒的文件,用普通的RandomAccessFile需要20多秒。
channelOut = randomAccessFile.getChannel();
// 內(nèi)存映射,直接使用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. 測(cè)試demo
清單文件中添加網(wǎng)絡(luò)權(quán)限和文件訪問權(quán)限
<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:
// 新下載前清空斷點(diǎn)信息
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();
// 存儲(chǔ)此時(shí)的totalBytes,即斷點(diǎn)位置。
breakPoints = totalBytes;
break;
case R.id.continue_button:
downloader.download(breakPoints);
break;
}
}
@Override
public void onPreExecute(long contentLength) {
// 文件總長(zhǎng)只需記錄一次,要注意斷點(diǎn)續(xù)傳后的contentLength只是剩余部分的長(zhǎng)度
if (this.contentLength == 0L) {
this.contentLength = contentLength;
progressBar.setMax((int) (contentLength / 1024));
}
}
@Override
public void update(long totalBytes, boolean done) {
// 注意加上斷點(diǎn)的長(zhǎng)度
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();
}
}
}
最后是動(dòng)態(tài)效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中Okhttp3實(shí)現(xiàn)上傳多張圖片同時(shí)傳遞參數(shù)
- Android OkHttp Post上傳文件并且攜帶參數(shù)實(shí)例詳解
- 使用Android的OkHttp包實(shí)現(xiàn)基于HTTP協(xié)議的文件上傳下載
- Android中實(shí)現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度
- android中okhttp實(shí)現(xiàn)斷點(diǎn)上傳示例
- Android使用OkHttp上傳圖片的實(shí)例代碼
- RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳
- android通過okhttpClient下載網(wǎng)頁內(nèi)容的實(shí)例代碼
- android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
- Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
相關(guān)文章
Android多功能視頻播放器GSYVideoPlayer開發(fā)流程
怎么在Android中實(shí)現(xiàn)GSYVideoPlayer視頻播放器?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲2022-11-11
Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請(qǐng)求的壓縮實(shí)例詳解
這篇文章主要介紹了Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請(qǐng)求的壓縮實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android 開機(jī)自啟動(dòng)Service實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android 開機(jī)自啟動(dòng)Service實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android如何調(diào)整線程調(diào)用棧大小
這篇文章主要介紹了Android如何調(diào)整線程調(diào)用棧大小,幫助大家更好的進(jìn)行Android開發(fā),完善自身程序,感興趣的朋友可以了解下2020-10-10
Android?JetPack組件的支持庫Databinding詳解
DataBinding是Google發(fā)布的一個(gè)數(shù)據(jù)綁定框架,它能夠讓開發(fā)者減少重復(fù)性非常高的代碼,如findViewById這樣的操作。其核心優(yōu)勢(shì)是解決了數(shù)據(jù)分解映射到各個(gè)view的問題,在MVVM框架中,實(shí)現(xiàn)的View和Viewmode的雙向數(shù)據(jù)綁定2022-08-08
Android動(dòng)態(tài)添加碎片代碼實(shí)例
這篇文章主要介紹了Android動(dòng)態(tài)添加碎片代碼實(shí)例,2019-06-06
Android中的SQLite數(shù)據(jù)庫簡(jiǎn)介
SQLite是Android系統(tǒng)采用的一種開源的輕量級(jí)的關(guān)系型的數(shù)據(jù)庫。這篇文章主要介紹了Android中的SQLite數(shù)據(jù)庫簡(jiǎn)介,需要的朋友可以參考下2017-03-03
Android 圖文詳解Binder進(jìn)程通信底層原理
Android系統(tǒng)中,多進(jìn)程間的通信都是依賴于底層Binder IPC機(jī)制,Binder機(jī)制是一種RPC方案。例如:當(dāng)進(jìn)程A中的Activity與進(jìn)程B中的Service通信時(shí),就使用了binder機(jī)制2021-10-10
Android使用MediaRecorder類進(jìn)行錄制視頻
這篇文章主要介紹了Android使用MediaRecorder類進(jìn)行錄制視頻的相關(guān)資料,需要的朋友可以參考下2015-10-10

