
本文共 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
���������������������
@Overridepublic 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
���������������������������������
readByteArray()
���������- ������
buffer.writeAll(source)
��������������������������� - ������������
buffer.readByteArray()
���������������
@Overridepublic byte[] readByteArray() throws IOException { buffer.writeAll(source); return buffer.readByteArray();}
���������������
������������
���������������������������������������������������������������OkHttp
���������������������������������������������������������������������������������������������������������������������OkHttp
������������������������������������������������������������������