Cronet リクエストのライフサイクル
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Cronet を使用して作成されたリクエストのライフサイクルと、ライブラリが提供するコールバック メソッドを使用してリクエストを管理する方法について説明します。
ライフサイクルの概要
Cronet ライブラリを使用して作成されたネットワーク リクエストは、UrlRequest
クラスで表されます。UrlRequest
のライフサイクルを理解するには、次のコンセプトが重要です。
- 状態
- 状態とは、特定の時点でリクエストが発生している特定の条件です。Cronet ライブラリを使用して作成された UrlRequest オブジェクトは、そのライフサイクル内でさまざまな状態に遷移します。リクエストのライフサイクルには、初期状態、複数の移行状態、最終状態があります。
UrlRequest
メソッド
- クライアントは、状態に応じて
UrlRequest
オブジェクトの特定のメソッドを呼び出すことができます。このメソッドは、リクエストを別の状態に移動します。 Callback
メソッド
UrlRequest.Callback
クラスのメソッドを実装することで、アプリはリクエストの進捗状況に関する最新情報を受け取ることができます。ライフサイクルを状態から別の状態に遷移する UrlRequest
オブジェクトのメソッドを呼び出すコールバック メソッドを実装できます。
次のリストは、UrlRequest
ライフサイクルのフローを示しています。
- アプリが
start()
メソッドを呼び出した後、ライフサイクルは開始状態になります。
- サーバーがリダイレクト レスポンスを送信すると、
onRedirectReceived()
メソッドにフローが移ります。この方法では、次のいずれかのクライアント アクションを行うことができます。
followRedirect()
を使用してリダイレクトに従います。このメソッドは、リクエストを Started 状態に戻します。
cancel()
を使用してリクエストをキャンセルします。
このメソッドはリクエストを onCanceled()
メソッドに受け渡します。このメソッドは、リクエストが Canceled の最終状態に移行される前に、アプリで追加のオペレーションを実行できます。
- アプリがすべてのリダイレクトをたどった後、サーバーがレスポンス ヘッダーを送信し、
onResponseStarted()
メソッドが呼び出されます。リクエストは Waiting for read() 状態になります。アプリは read()
メソッドを呼び出して、レスポンス本文の一部の読み取りを試みる必要があります。read()
が呼び出されると、リクエストは Reading 状態になります。次のような結果になる可能性があります。
- 読み取りアクションは成功しましたが、使用可能なデータがさらにあります。
onReadCompleted()
が呼び出され、リクエストは再び Wait for read() 状態になります。アプリは read()
メソッドを再度呼び出して、レスポンス本文の読み取りを続行する必要があります。また、アプリで cancel()
メソッドを使用してリクエストの読み取りを停止することもできます。
- 読み取りアクションは成功し、これ以上データがありません。
onSucceeded()
メソッドが呼び出され、リクエストが成功最終状態になります。
- 読み取りアクションが失敗しました。
onFailed
メソッドが呼び出され、リクエストの最終状態は「失敗」になります。
次の図は、UrlRequest
オブジェクトのライフサイクルを示しています。

Cronet リクエストのライフサイクル
凡例 |
|
初期状態 |
最終状態 |
遷移状態 |
コールバック メソッド |
UrlRequest メソッド |
|
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[null,null,["最終更新日 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 | |"]]