android断点续传okhttp,Android开发记录-基于okhttp的断点续传

需求

基于okhttp实现文件断点续传

一、okhttp

省略okhttp依赖和使用方式。

二、实现

1、interface

import java.io.IOException;

import okhttp3.Call;

import okhttp3.Response;

public interface HttpDownloadListener{

void onFailure(Call call, IOException e,long totalLength, long downloadLength);

void inProgress(Call call, Response response, long totalLength, long downloadLength);

void onResponse(Call call, Response response, long totalLength, long downloadLength);

}

2、OkHttpDownloadUtil

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import okhttp3.Call;

import okhttp3.Callback;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

import okhttp3.ResponseBody;

public class OkHttpDownloadUtil {

private final String TAG = this.getClass().getSimpleName();

private Call mCall;

private String downloadUrl;//url

private long downloadLength = 0;//已经下载长度

private long totalLength = 0;//整体文件大小

private File file;//保存文件

private HttpDownListener mHttpDownListener;//下载进度接口回调

public void getDownloadRequest(final String downloadUrl, final File file, Long already,Long total, final HttpDownloadListener listener){

this.downloadUrl = downloadUrl;

this.downloadLength = already;

this.totalLength = total;

this.file = file;

this.mHttpDownListener = listener;

OkHttpClient okHttpClient = new OkHttpClient();

Request request = new Request.Builder()

.url(downloadUrl)

.header("RANGE", "bytes=" + downloadLength + "-")

.build();

mCall = okHttpClient.newCall(request);

//发送请求

mCall.enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

if (mHttpDownListener != null) {

mHttpDownListener.onFailure(call, e,totalLength, downloadLength);

}

}

@Override

public void onResponse(Call call, Response response) throws IOException {

ResponseBody responseBody = response.body();

InputStream inputStream = responseBody.byteStream();

RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

if (totalLength == 0){

totalLength = responseBody.contentLength();

randomAccessFile.setLength(totalLength);

}

if (downloadLength != 0){

randomAccessFile.seek(downloadLength);

}

byte[] bytes = new byte[2048];

int len = 0;

try {

while ((len = inputStream.read(bytes)) != -1) {

randomAccessFile.write(bytes,0,len);

downloadLength = downloadLength + len;

if (mHttpDownListener != null) {

mHttpDownListener.inProgress(call, response, totalLength, downloadLength);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

downloadLength = randomAccessFile.getFilePointer();

randomAccessFile.close();

inputStream.close();

if (mHttpDownListener != null) {

mHttpDownListener.onResponse(call, response, totalLength, downloadLength);

}

}

}

});

}

/**

* 停止下载

*/

public void stopDownload(){

if (mCall!=null){

mCall.cancel();

}

}

}

3、使用

OkHttpDownUtil okHttpDownUtil = new OkHttpDownUtil();

okHttpDownUtil.getDownloadRequest(url, file, already,total,new HttpDownloadListener() {

@Override

public void onFailure(Call call, IOException e,long mTotalLength,long mAlreadyDownLength) {

//

}

@Override

public void inProgress(Call call, Response response, long mTotalLength, long mAlreadyDownLength) {

//

}

@Override

public void onResponse(Call call, Response response, long mTotalLength, long mAlreadyDownLength){

//

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值