public abstract class ResponseBody extends Object implements Closeable
Both this class and Response
implement Closeable
. Closing a response simply
closes its response body. If you invoke Call.execute()
or implement Callback.onResponse(okhttp3.Call, okhttp3.Response)
you must close this body by calling any of the following methods:
There is no benefit to invoking multiple close()
methods for the same response body.
For synchronous calls, the easiest way to make sure a response body is closed is with a try
block. With this structure the compiler inserts an implicit finally
clause that
calls close()
for you.
Call call = client.newCall(request);
try (Response response = call.execute()) {
... // Use the response.
}
You can use a similar block for asynchronous calls:
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
... // Use the response.
}
}
public void onFailure(Call call, IOException e) {
... // Handle the failure.
}
});
These examples will not work if you're consuming the response body on another thread. In such
cases the consuming thread must call close()
when it has finished reading the response
body.
This class may be used to stream very large responses. For example, it is possible to use this class to read a response that is larger than the entire memory allocated to the current process. It can even stream a response larger than the total storage on the current device, which is a common requirement for video streaming applications.
Because this class does not buffer the full response in memory, the application may not
re-read the bytes of the response. Use this one shot to read the entire response into memory with
bytes()
or string()
. Or stream the response with either source()
,
byteStream()
, or charStream()
.
Constructor and Description |
---|
ResponseBody() |
Modifier and Type | Method and Description |
---|---|
byte[] |
bytes()
Returns the response as a byte array.
|
InputStream |
byteStream() |
Reader |
charStream()
Returns the response as a character stream.
|
void |
close() |
abstract long |
contentLength()
Returns the number of bytes in that will returned by
bytes() , or byteStream() , or
-1 if unknown. |
abstract MediaType |
contentType() |
static ResponseBody |
create(MediaType contentType,
byte[] content)
Returns a new response body that transmits
content . |
static ResponseBody |
create(MediaType contentType,
okio.ByteString content)
Returns a new response body that transmits
content . |
static ResponseBody |
create(MediaType contentType,
long contentLength,
okio.BufferedSource content)
Returns a new response body that transmits
content . |
static ResponseBody |
create(MediaType contentType,
String content)
Returns a new response body that transmits
content . |
abstract okio.BufferedSource |
source() |
String |
string()
Returns the response as a string.
|
public abstract long contentLength()
bytes()
, or byteStream()
, or
-1 if unknown.public final InputStream byteStream()
public abstract okio.BufferedSource source()
public final byte[] bytes() throws IOException
This method loads entire response body into memory. If the response body is very large this
may trigger an OutOfMemoryError
. Prefer to stream the response body if this is a
possibility for your response.
IOException
public final Reader charStream()
If the response starts with a Byte Order Mark (BOM), it is consumed and used to determine the charset of the response bytes.
Otherwise if the response has a Content-Type header that specifies a charset, that is used to determine the charset of the response bytes.
Otherwise the response bytes are decoded as UTF-8.
public final String string() throws IOException
If the response starts with a Byte Order Mark (BOM), it is consumed and used to determine the charset of the response bytes.
Otherwise if the response has a Content-Type header that specifies a charset, that is used to determine the charset of the response bytes.
Otherwise the response bytes are decoded as UTF-8.
This method loads entire response body into memory. If the response body is very large this
may trigger an OutOfMemoryError
. Prefer to stream the response body if this is a
possibility for your response.
IOException
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public static ResponseBody create(@Nullable MediaType contentType, String content)
content
. If contentType
is non-null
and lacks a charset, this will use UTF-8.public static ResponseBody create(@Nullable MediaType contentType, byte[] content)
content
.public static ResponseBody create(@Nullable MediaType contentType, okio.ByteString content)
content
.public static ResponseBody create(@Nullable MediaType contentType, long contentLength, okio.BufferedSource content)
content
.Copyright © 2019. All rights reserved.