Chu kỳ yêu cầu Cronet
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tìm hiểu về vòng đời của các yêu cầu được tạo bằng Cronet và cách quản lý các yêu cầu đó bằng các phương thức gọi lại do thư viện cung cấp.
Tổng quan về vòng đời
Các yêu cầu mạng được tạo bằng Thư viện Cronet được biểu thị bằng lớp UrlRequest
.
Bạn cần nắm được các khái niệm quan trọng sau đây về vòng đời của UrlRequest
:
- Tiểu bang
- Trạng thái là một điều kiện cụ thể mà yêu cầu xảy ra tại một thời điểm cụ thể.
Các đối tượng UrlRequest được tạo bằng Thư viện Cronet di chuyển qua nhiều trạng thái trong vòng đời của chúng. Vòng đời yêu cầu bao gồm trạng thái ban đầu cũng như nhiều trạng thái chuyển tiếp và trạng thái cuối cùng.
- Phương thức
UrlRequest
- Ứng dụng có thể gọi các phương thức cụ thể trên các đối tượng
UrlRequest
tuỳ thuộc vào trạng thái. Các phương thức này sẽ chuyển yêu cầu từ trạng thái này sang trạng thái khác.
- Phương thức
Callback
- Bằng cách triển khai các phương thức của lớp
UrlRequest.Callback
, ứng dụng của bạn có thể nhận được thông tin cập nhật về tiến trình của yêu cầu. Bạn có thể triển khai các phương thức gọi lại để gọi các phương thức của đối tượng UrlRequest
. Các phương thức này sẽ thực hiện vòng đời từ một trạng thái sang một trạng thái khác.
Danh sách sau đây mô tả luồng của vòng đời UrlRequest
:
- Vòng đời ở trạng thái Started (Đã bắt đầu) sau khi ứng dụng của bạn gọi phương thức
start()
.
- Máy chủ có thể gửi phản hồi chuyển hướng. Phản hồi này sẽ đưa luồng đến phương thức
onRedirectReceived()
. Trong phương thức này, bạn có thể thực hiện một trong các hành động sau đây của ứng dụng:
- Đi theo lệnh chuyển hướng bằng cách sử dụng
followRedirect()
.
Phương thức này sẽ đưa yêu cầu trở về trạng thái Đã bắt đầu.
- Huỷ yêu cầu bằng cách sử dụng
cancel()
.
Phương thức này đưa yêu cầu đến phương thức onCanceled()
. Tại đây, ứng dụng có thể thực hiện thêm các thao tác trước khi yêu cầu được chuyển sang trạng thái cuối cùng Đã huỷ.
- Sau khi ứng dụng tuân theo tất cả các lệnh chuyển hướng, máy chủ sẽ gửi tiêu đề phản hồi và phương thức
onResponseStarted()
sẽ được gọi. Yêu cầu có trạng thái Đang chờ read(). Ứng dụng phải gọi phương thức read()
để cố gắng đọc một phần nội dung phản hồi. Sau khi read()
được gọi, yêu cầu sẽ ở trạng thái Đọc, trong đó có thể có những kết quả sau:
- Hành động đọc đã thành công, nhưng có nhiều dữ liệu hơn.
onReadCompleted()
được gọi và yêu cầu ở trạng thái Đang chờ read() một lần nữa.
Ứng dụng nên gọi lại phương thức read()
để tiếp tục đọc nội dung phản hồi. Ứng dụng cũng có thể ngừng đọc yêu cầu bằng cách dùng phương thức cancel()
.
- Hành động đọc đã thành công và không còn dữ liệu nào khác.
Phương thức
onSucceeded()
được gọi và yêu cầu hiện đang ở trạng thái cuối cùng Thành công.
- Không thực hiện được hành động đọc. Phương thức
onFailed
sẽ được gọi và trạng thái cuối cùng của yêu cầu hiện là Không thành công.
Sơ đồ dưới đây cho thấy vòng đời của một đối tượng UrlRequest
:

Vòng đời yêu cầu Cronet
Chú giải |
|
Trạng thái ban đầu của  |
trạng thái cuối cùng |
Trạng thái chuyển đổi  |
phương thức gọi lại |
phương thức UrlRequest |
|
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Cronet request lifecycle\n\nLearn about the lifecycle of requests created using Cronet and how to manage\nthem using the callback methods provided by the library.\n\nLifecycle overview\n------------------\n\nNetwork requests created using the Cronet Library are represented by the\n[`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) class.\nThe following concepts are important to understand the\n[`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) lifecycle:\n\n**States**\n: A state is the particular condition that the request is in at a specific time.\n [UrlRequest](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) objects created using the Cronet\n Library move through different states in their lifecycle. The request\n lifecycle includes an initial state, and multiple transitional and final\n states.\n\n**`UrlRequest` methods**\n: Clients can call specific methods on\n [`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) objects depending on the\n state. The methods move the request from one state to another.\n\n**`Callback` methods**\n: By implementing methods of the\n [`UrlRequest.Callback`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback) class, your\n app can receive updates about the progress of the request. You can implement\n the callback methods to call methods of the\n [`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) object that take the lifecycle\n from a state to another.\n\nThe following list describes the flow of the\n[`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) lifecycle:\n\n1. The lifecycle is in the **Started** state after your app calls the [`start()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#start()) method.\n2. The server could send a redirect response, which takes the flow to the [`onRedirectReceived()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onRedirectReceived(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo,%20java.lang.String)) method. In this method, you can take one of the following client actions:\n - Follow the redirect using [`followRedirect()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#followRedirect()). This method takes the request back to the **Started** state.\n - Cancel the request using [`cancel()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#cancel()). This method takes the request to the [`onCanceled()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onCanceled(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo)) method where the app can perform additional operations before the request is moved to the **Canceled** final state.\n3. After the app follows all the redirects, the server sends the response headers and the [`onResponseStarted()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onResponseStarted(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo)) method is called. The request is in the **Waiting for read()** state. The app should call the [`read()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#read(java.nio.ByteBuffer)) method to attempt to read part of the response body. After `read()` is called, the request is in the **Reading** state, where there are the following possible outcomes:\n - The reading action was successful, but there is more data available. The [`onReadCompleted()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onReadCompleted(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo,%20java.nio.ByteBuffer)) is called and the request is in the **Waiting for read()** state again. The app should call the [`read()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#read(java.nio.ByteBuffer)) method again to continue reading the response body. The app could also stop reading the request by using the [`cancel()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest#cancel()) method .\n - The reading action was successful, and there is no more data available. The [`onSucceeded()`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onSucceeded(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo)) method is called and the request is now in the **Succeeded** final state.\n - The reading action failed. The [`onFailed`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest.Callback#onFailed(org.chromium.net.UrlRequest,%20org.chromium.net.UrlResponseInfo,%20org.chromium.net.CronetException)) method is called and the final state of the request is now **Failed**.\n\nThe following diagram shows the lifecycle of a\n[`UrlRequest`](/develop/connectivity/cronet/reference/org/chromium/net/UrlRequest) object:\n\n\u003cbr /\u003e\n\n\nThe Cronet request lifecycle\n\n| Legend | |\n|----------------------|------------------|\n| initial state | final state |\n| transitional state | callback methods |\n| `UrlRequest` methods | |"]]