每天记录学习的新知识 :OKHTTP 下载进度计算
发布日期:2021-05-10 05:21:24 浏览次数:20 分类:精选文章

本文共 3802 字,大约阅读时间需要 12 分钟。

ResponseBody���������������������������������������������

���������������������ResponseBody

���Android������������OkHttp���������������������������������RequestBody���ResponseBody������������������������������������ResponseBody������������������������Head���Body������������������������������������������������������������������������������������ResponseBody������������������������������������������

���������������ResponseBody

ResponseBody���������������������������������������������������������������������������

public abstract class ResponseBody implements Closeable {
@Override
public abstract @Nullable MediaType contentType();
@Override
public abstract long contentLength();
@Override
public abstract BufferedSource source();
}

���������BufferedSource���������Okio.buffer(Source)���������������Source������responseBody.source()���������������������ForwardingSource���������������������

@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);
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
progressListener.update(
totalBytesRead,
responseBody.contentLength(),
bytesRead == -1
);
return bytesRead;
}
};
}

������������������

���OkHttp������������������������������������ResponseBody���

.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response.newBuilder()
.body(new ProgressResponseBody(response.body(), listener))
.build();
}
});

������������Callback������������

call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, Response response) throws IOException {
// ������������������������
response.body().bytes(); // ��� response.body().string();
}
});

������������������

  • bytes()���������
  • public final byte[] bytes() throws IOException {
    long contentLength = contentLength();
    if (contentLength > Integer.MAX_VALUE) {
    throw new IOException("Cannot buffer entire body for content length: " + contentLength);
    }
    BufferedSource source = source();
    byte[] bytes;
    try {
    bytes = source.readByteArray();
    } finally {
    Util.closeQuietly(source);
    }
    if (contentLength != -1 && contentLength != bytes.length) {
    throw new IOException("Content-Length (" + contentLength + ") and stream length (" + bytes.length + ") disagree");
    }
    return bytes;
    }

    ���������������

    • ������contentLength()���������������������
    • ������source.readByteArray()���������������
    • ���������������������������������������������������������������������Content-Length���������������������������������
    1. readByteArray()���������
    2. @Override
      public byte[] readByteArray() throws IOException {
      buffer.writeAll(source);
      return buffer.readByteArray();
      }

      ���������������

      • ������buffer.writeAll(source)���������������������������
      • ������������buffer.readByteArray()���������������

      ������������

      ���������������������������������������������������������������OkHttp���������������������������������������������������������������������������������������������������������������������OkHttp������������������������������������������������������������������

    上一篇:每天记录学习的新知识 :Retrofit2过一遍
    下一篇:每天记录学习的新知识 :OKHTTP的拦截器了解下

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月25日 04时02分11秒