مراحل نشاط طلب Cronet
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تعرف على دورة حياة الطلبات التي تم إنشاؤها باستخدام Cronet وكيفية إدارتها باستخدام طرق معاودة الاتصال التي توفرها المكتبة.
نظرة عامة على مراحل النشاط
تمثِّل الفئة UrlRequest
طلبات الشبكة التي تم إنشاؤها باستخدام مكتبة Cronet.
من المهم فهم المفاهيم التالية حول مراحل نشاط
UrlRequest
:
- الحالات
- الحالة هي الحالة المحدّدة التي يكون فيها الطلب في فترة محدّدة.
تنتقل عناصر UrlRequest التي تم إنشاؤها باستخدام مكتبة
كرونت عبر حالات مختلفة خلال مراحل نشاطها. تتضمن دورة
حياة الطلب حالة أولية وحالات انتقالية ونهائية متعددة.
- طريقتان (
UrlRequest
)
- يمكن للعملاء استدعاء طرق محددة في كائنات
UrlRequest
بناءً على الحالة. تعمل الطريقتان على نقل الطلب من حالة إلى أخرى.
- طريقتان (
Callback
)
- من خلال تنفيذ طُرق الفئة
UrlRequest.Callback
، يمكن أن يتلقّى تطبيقك آخر الأخبار عن مستوى تقدّم الطلب. يمكنك تنفيذ طرق استدعاء الدوال البرمجية لاستدعاء طرق كائن UrlRequest
الذي ينقل مراحل النشاط
من حالة إلى أخرى.
تصف القائمة التالية سير عمل
UrlRequest
:
- تكون مراحل النشاط في حالة تم البدء بعد أن يطلب تطبيقك طريقة
start()
.
- يمكن أن يرسل الخادم استجابة إعادة التوجيه، والتي تأخذ التدفق إلى الإجراء
onRedirectReceived()
. وبهذه الطريقة، يمكنك اتّخاذ أحد الإجراءات التالية للعميل:
- اتبِع عملية إعادة التوجيه باستخدام
followRedirect()
.
تعيد هذه الطريقة الطلب إلى الحالة Started (تم البدء).
- يمكنك إلغاء الطلب باستخدام
cancel()
.
تأخذ هذه الطريقة الطلب إلى طريقة
onCanceled()
حيث يمكن للتطبيق إجراء عمليات إضافية قبل نقل الطلب
إلى الحالة النهائية ملغاة.
- بعد أن يتّبع التطبيق جميع عمليات إعادة التوجيه، يرسل الخادم عناوين الاستجابة ويُستدعى الإجراء
onResponseStarted()
. الطلب في حالة انتظار القراءة(). يجب أن يستدعي التطبيق الطريقة read()
لمحاولة قراءة جزء من نص الاستجابة. بعد استدعاء read()
، يكون الطلب في حالة القراءة، حيث تظهر النتائج المحتملة التالية:
- تمّت عملية القراءة بنجاح، ولكن تتوفّر المزيد من البيانات. يتم استدعاء
onReadCompleted()
ويصبح الطلب في حالة انتظار القراءة() مرة أخرى.
يجب أن يستدعي التطبيق الطريقة
read()
مرة أخرى لمواصلة قراءة نص الاستجابة. ويمكن أيضًا للتطبيق التوقف عن قراءة الطلب باستخدام طريقة cancel()
.
- تم إجراء القراءة بنجاح، ولا تتوفّر بيانات أخرى.
تم استدعاء الطريقة
onSucceeded()
ويصبح الطلب الآن في الحالة النهائية تم بنجاح.
- تعذَّر إجراء القراءة. تم استدعاء الإجراء
onFailed
وأصبحت الحالة النهائية للطلب الآن تعذّر.
يوضّح المخطّط التالي دورة حياة عنصر UrlRequest
:

مراحل تنفيذ طلبات Cronet
أسطورة |
|
حالة أولية واحدة ( ) |
حالة نهائية واحدة ( ) |
حالة انتقالية واحدة ( ) |
طريقة لرد الاتصال |
طرق UrlRequest |
|
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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 | |"]]