מחזור החיים של בקשה ב-Cronet
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
במאמר הזה מוסבר על מחזור החיים של בקשות שנוצרו באמצעות Cronet ואיך לנהל אותן באמצעות שיטות הקריאה החוזרת שסופקו על ידי הספרייה.
סקירה כללית על מחזור החיים
בקשות רשת שנוצרו באמצעות ספריית Cronet מיוצגות על ידי המחלקה UrlRequest
.
חשוב להבין את המושגים הבאים כדי להבין את מחזור החיים של UrlRequest
:
- מדינות
- מצב הוא התנאי הספציפי שבו הבקשה נמצאת בזמן מסוים.
אובייקטים מסוג UrlRequest שנוצרו באמצעות ספריית Cronet עוברים בין מצבים שונים במחזור החיים שלהם. מחזור החיים של הבקשה כולל מצב ראשוני, ומצבי מעבר ומצבים סופיים רבים.
UrlRequest
אמצעי תשלום
- לקוחות יכולים לקרוא לשיטות ספציפיות באובייקטים של
UrlRequest
בהתאם למצב. השיטות מעבירות את הבקשה ממצב אחד למצב אחר.
Callback
אמצעי תשלום
- הטמעה של שיטות מהסיווג
UrlRequest.Callback
מאפשרת לאפליקציה לקבל עדכונים לגבי התקדמות הבקשה. אפשר להטמיע את שיטות הקריאה החוזרת כדי לקרוא לשיטות של אובייקט UrlRequest
שמקבלות את מחזור החיים ממצב אחד למצב אחר.
ברשימה הבאה מתואר התהליך של מחזור החיים של UrlRequest
:
- מחזור החיים נמצא במצב Started אחרי שהאפליקציה קוראת לשיטה
start()
.
- השרת יכול לשלוח תגובה להפניה אוטומטית, שמעבירה את התהליך לשיטה
onRedirectReceived()
. בשיטה הזו, אפשר לבצע אחת מהפעולות הבאות בצד הלקוח:
- עוקבים אחרי ההפניה לכתובת אחרת באמצעות
followRedirect()
.
השיטה הזו מחזירה את הבקשה למצב Started.
- לבטל את הבקשה באמצעות
cancel()
.
השיטה הזו מעבירה את הבקשה לשיטה onCanceled()
, שבה האפליקציה יכולה לבצע פעולות נוספות לפני שהבקשה עוברת למצב הסופי בוטלה.
- אחרי שהאפליקציה מבצעת את כל ההפניות האוטומטיות, השרת שולח את כותרות התגובה והשיטה
onResponseStarted()
נקראת. הבקשה במצב המתנה לקריאה(). האפליקציה צריכה להפעיל את השיטה read()
כדי לנסות לקרוא חלק מגוף התשובה. אחרי שקוראים ל-read()
, הבקשה נמצאת במצב קריאה, ויש לה את התוצאות האפשריות הבאות:
- פעולת הקריאה בוצעה בהצלחה, אבל יש עוד נתונים זמינים. הפונקציה
onReadCompleted()
מופעלת והבקשה חוזרת למצב בהמתנה לקריאה().
האפליקציה צריכה להפעיל שוב את השיטה read()
כדי להמשיך לקרוא את גוף התגובה. האפליקציה יכולה גם להפסיק לקרוא את הבקשה באמצעות השיטה cancel()
.
- פעולת הקריאה הושלמה בהצלחה, ואין יותר נתונים זמינים.
השיטה
onSucceeded()
מופעלת והבקשה נמצאת עכשיו במצב הסופי Succeeded.
- הפעולה של הקריאה נכשלה. השיטה
onFailed
מופעלת והמצב הסופי של הבקשה הוא נכשל.
התרשים הבא מציג את מחזור החיים של אובייקט UrlRequest
:

מחזור החיים של בקשת Cronet
Legend |
|
מצב התחלתי |
סטטוס סופי |
מצב מעבר |
שיטות קריאה חוזרת |
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 | |"]]