چرخه عمر درخواست کرونت
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در مورد چرخه عمر درخواست های ایجاد شده با استفاده از Cronet و نحوه مدیریت آنها با استفاده از روش های پاسخ به تماس ارائه شده توسط کتابخانه بیاموزید.
مروری بر چرخه حیات
درخواست های شبکه ایجاد شده با استفاده از کتابخانه Cronet توسط کلاس UrlRequest
نمایش داده می شود. مفاهیم زیر برای درک چرخه عمر UrlRequest
مهم هستند:
- ایالات
- حالت، شرایط خاصی است که درخواست در یک زمان خاص در آن قرار دارد. اشیاء UrlRequest ایجاد شده با استفاده از کتابخانه Cronet در حالت های مختلف در چرخه حیات خود حرکت می کنند. چرخه عمر درخواست شامل یک حالت اولیه و چندین حالت انتقالی و نهایی است.
- روش های
UrlRequest
- کلاینت ها می توانند بسته به حالت، متدهای خاصی را روی اشیاء
UrlRequest
فراخوانی کنند. متدها درخواست را از یک حالت به حالت دیگر منتقل می کنند. - روش های
Callback
- با پیادهسازی روشهای کلاس
UrlRequest.Callback
، برنامه شما میتواند بهروزرسانیهایی درباره پیشرفت درخواست دریافت کند. برای فراخوانی متدهای شیء UrlRequest
که چرخه حیات را از حالتی به حالت دیگر می برد، می توانید متدهای callback را پیاده سازی کنید.
لیست زیر جریان چرخه عمر UrlRequest
را شرح می دهد:
- پس از اینکه برنامه شما متد
start()
را فراخوانی کرد، چرخه حیات در حالت Started است. - سرور می تواند یک پاسخ ریدایرکت ارسال کند که جریان را به متد
onRedirectReceived()
می برد. در این روش می توانید یکی از اقدامات مشتری زیر را انجام دهید:- ریدایرکت را با استفاده از
followRedirect()
دنبال کنید. این روش درخواست را به حالت Started برمی گرداند. - درخواست را با استفاده از
cancel()
لغو کنید. این روش درخواست را به متد onCanceled()
می برد که در آن برنامه می تواند قبل از اینکه درخواست به حالت نهایی Canceled منتقل شود، عملیات اضافی انجام دهد.
- پس از اینکه برنامه تمام ریدایرکت ها را دنبال کرد، سرور سرصفحه های پاسخ را ارسال می کند و متد
onResponseStarted()
فراخوانی می شود. درخواست در حالت Waiting for read() است. برنامه باید متد read()
را فراخوانی کند تا بخشی از بدنه پاسخ را بخواند. پس از فراخوانی read()
درخواست در حالت Reading قرار می گیرد که در آن نتایج احتمالی زیر وجود دارد:- عمل خواندن موفقیت آمیز بود، اما داده های بیشتری در دسترس است.
onReadCompleted()
فراخوانی می شود و درخواست دوباره در حالت Waiting for read() قرار می گیرد. برای ادامه خواندن بدنه پاسخ، برنامه باید متد read()
را دوباره فراخوانی کند. این برنامه همچنین می تواند با استفاده از روش cancel()
خواندن درخواست را متوقف کند. - عمل خواندن با موفقیت انجام شد و داده دیگری در دسترس نیست. متد
onSucceeded()
فراخوانی می شود و درخواست اکنون در وضعیت نهایی Succeeded قرار دارد. - عمل خواندن ناموفق بود. متد
onFailed
فراخوانی می شود و وضعیت نهایی درخواست اکنون Failed است.
نمودار زیر چرخه حیات یک شی UrlRequest
را نشان می دهد:

چرخه عمر درخواست کرونت
افسانه | |
---|
حالت اولیه | حالت نهایی |
حالت انتقالی | روش های برگشت به تماس |
روش های UrlRequest | |
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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 | |"]]