默认情况下,Retrofit在处理结果前会将整个Server Response读进内存,这在JSON或者XML等Response上表现还算良好,但如果是一个非常大的文件,就可能造成OutofMemory异常。因此我们在进行下载大文件时需要使用@Streaming注解,使用@Streaming主要作用是把实时下载的字节就立马写入磁盘,而不用把整个文件读入内存。
final ExecutorService executorService = Executors.newFixedThreadPool(1);
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.callbackExecutor(executorService) android.os.NetworkOnMainThreadException
.baseUrl("https://raw.githubusercontent.com/");
在Retrofit中callback回调默认在主线程,使用@Streaming必须把callback回调放在子线程中,这里增加callbackExecutor(executorService) 表示把callback回调放在核心线程为1的线程池中执行。
实现进度监听首先需要创建一个监听进度的回调接口:
/**
* progress表示当前已经下载的文件大小
* total表示文件大小
* speed表示下载速度
* done表示是否下载完成
* Created by 1013369768 on 2017/10/20.
*/
public interface ProgressListener {
void onProgress(long progress,long total,long speed,boolean done);
}
重写ResponseBody类的某些方法:
public class ProgressResponseBody extends ResponseBody {
private final ResponseBody responseBody;
private final ProgressListener progressListener;
private BufferedSource bufferedSource;
public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
this.responseBody = responseBody;
this.progressListener = progressListener;
}
@Nullable
@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 totalBytesRead = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
//当前读取字节数
long bytesRead = super.read(sink, byteCount);
//增加当前读取的字节数,如果读取完成了bytesRead会返回-1
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
//回调,如果contentLength()不知道长度,会返回-1
progressListener.onProgress(totalBytesRead, responseBody.contentLength(),bytesRead,bytesRead == -1);
return bytesRead;
}
};
}
}
在ProgressResponseBody类中,把已经读取的字节数totalBytesRead,文件的总大小responseBody.contentLength(),读取的速度bytesRead,bytesRead==-1表示读取到文件结尾返回true,未读取到文件结尾返回false;得到这些数值后传入到ProgressListener接口,所以这个ProgressListener接口是在子线程中运行的。
public class ProgressHelper {
private static ProgressBean progressBean = new ProgressBean();
private static ProgressHandler mProgressHandler;
public static OkHttpClient.Builder addProgress(OkHttpClient.Builder builder){
if (builder == null){
builder = new OkHttpClient.Builder();
}
final ProgressListener progressListener = new ProgressListener() {
//该方法在子线程中运行
@Override
public void onProgress(long progress, long total,long speed, boolean done) {
if (mProgressHandler == null){
return;
}
progressBean.setBytesRead(progress);
progressBean.setContentLength(total);
progressBean.setSpeed(speed);
progressBean.setDone(done);
mProgressHandler.sendMessage(progressBean);
}
};
//添加拦截器,自定义ResponseBody,添加下载进度
builder.networkInterceptors().add(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(
new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
});
return builder;
}
public static void setProgressHandler(ProgressHandler progressHandler){
mProgressHandler = progressHandler;
}
}
我们通过为OkhttpClient添加一个拦截器来使用我们自定义的ProgressResponseBody。并且在ProgressHelper类中把long progress, long total,long speed, boolean done参数保存在progressBean类中。
public class ProgressBean {
private long bytesRead;
private long contentLength;
private long speed;
private boolean done;
public long getSpeed() {
return speed;
}
public void setSpeed(long speed) {
this.speed = speed;
}
public long getBytesRead() {
return bytesRead;
}
public void setBytesRead(long bytesRead) {
this.bytesRead = bytesRead;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
}
由于onProgress方法回调在子线程中,所以需要使用handler进行更新UI
public abstract class ProgressHandler {
private static final int DOWNLOAD_PROGRESS = 1;
protected abstract void onProgress(long progress, long total,long speed, boolean done);
private final Handler mHandler = new UIHandler(Looper.getMainLooper(),this);
protected void sendMessage(ProgressBean progressBean){
mHandler.obtainMessage(DOWNLOAD_PROGRESS,progressBean).sendToTarget();
}
static class UIHandler extends Handler{
private final WeakReference mProgressHandler;
public UIHandler(Looper looper,ProgressHandler progressHandler) {
super(looper);
mProgressHandler = new WeakReference(progressHandler);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case DOWNLOAD_PROGRESS:
ProgressHandler progressHandler = mProgressHandler.get();
if(progressHandler!=null) {
ProgressBean progressBean = (ProgressBean)msg.obj;
progressHandler.onProgress(progressBean.getBytesRead(),progressBean.getContentLength(),progressBean.getSpeed(),progressBean.isDone());
}
break;
default:
break;
}
}
}
}
涉及到消息机制就涉及到Handler类,在Handler的子类中维护一个弱引用指向外部类(用到了static防止内存泄露,但是需要调用外部类的一个非静态函数,所以将外部类引用直接由构造函数传入,在内部通过调用该引用的方法去实现),然后将主线程的Looper传入,调用父类构造函数,在handleMessage函数中回调我们的抽象方法。