类 ResponseBody
- 所有已实现的接口:
Closeable,AutoCloseable
- 直接已知子类:
RealResponseBody
The response body must be closed.
Each response body is backed by a limited resource like a socket (live network responses) or an open file (for cached responses). Failing to close the response body will leak resources and may ultimately cause the application to slow down or crash.
Both this class and Response implement Closeable. Closing a response simply
closes its response body. If you invoke Call.execute() or implement Callback.onResponse(com.lark.oapi.okhttp.Call, com.lark.oapi.okhttp.Response) you must close this body by calling any of the following methods:
- Response.close()
- Response.body().close()
- Response.body().source().close()
- Response.body().charStream().close()
- Response.body().byteStream().close()
- Response.body().bytes()
- Response.body().string()
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.
The response body can be consumed only once.
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().
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明final byte[]bytes()Returns the response as a byte array.final InputStreamfinal ReaderReturns the response as a character stream.voidclose()abstract longReturns the number of bytes in that will returned bybytes(), orbyteStream(), or -1 if unknown.abstract MediaTypestatic ResponseBodyReturns a new response body that transmitscontent.static ResponseBodycreate(MediaType contentType, long contentLength, BufferedSource content) Returns a new response body that transmitscontent.static ResponseBodycreate(MediaType contentType, ByteString content) Returns a new response body that transmitscontent.static ResponseBodyReturns a new response body that transmitscontent.abstract BufferedSourcesource()final Stringstring()Returns the response as a string.
-
构造器详细资料
-
ResponseBody
public ResponseBody()
-
-
方法详细资料
-
create
Returns a new response body that transmitscontent. IfcontentTypeis non-null and lacks a charset, this will use UTF-8. -
create
Returns a new response body that transmitscontent. -
create
Returns a new response body that transmitscontent. -
create
public static ResponseBody create(@Nullable MediaType contentType, long contentLength, BufferedSource content) Returns a new response body that transmitscontent. -
contentType
-
contentLength
public abstract long contentLength()Returns the number of bytes in that will returned bybytes(), orbyteStream(), or -1 if unknown. -
byteStream
-
source
-
bytes
Returns the response as a byte array.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
-
charStream
Returns the response as a character stream.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.
-
string
Returns the response as a string.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
-
close
public void close()- 指定者:
close在接口中AutoCloseable- 指定者:
close在接口中Closeable
-