วงจรชีวิตของคำขอ Cronet
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ดูวงจรของคำขอที่สร้างขึ้นโดยใช้ Cronet และวิธีจัดการคำขอเหล่านั้น
โดยใช้วิธีการเรียกกลับที่ไลบรารีมีให้
ภาพรวมวงจร
คำขอเครือข่ายที่สร้างขึ้นโดยใช้ไลบรารี Cronet จะแสดงด้วยคลาส
UrlRequest
แนวคิดต่อไปนี้มีความสำคัญต่อการทำความเข้าใจวงจรUrlRequest
- รัฐ
- สถานะคือเงื่อนไขเฉพาะที่คำขออยู่ ณ เวลาใดเวลาหนึ่ง
ออบเจ็กต์ UrlRequest ที่สร้างขึ้นโดยใช้ไลบรารี Cronet
จะเปลี่ยนสถานะต่างๆ ในวงจรของออบเจ็กต์ วงจรคำขอ
ประกอบด้วยสถานะเริ่มต้น สถานะชั่วคราว และสถานะสุดท้าย
UrlRequest
วิธี
- ไคลเอ็นต์สามารถเรียกใช้เมธอดที่เฉพาะเจาะจงในออบเจ็กต์
UrlRequest
ได้โดยขึ้นอยู่กับสถานะ ซึ่งจะย้ายคำขอจากสถานะหนึ่งไปยังอีกสถานะหนึ่ง
Callback
วิธี
- การใช้วิธีการของคลาส
UrlRequest.Callback
จะช่วยให้แอปของคุณ
ได้รับการอัปเดตเกี่ยวกับความคืบหน้าของคำขอ คุณสามารถใช้
เมธอดเรียกกลับเพื่อเรียกเมธอดของออบเจ็กต์ UrlRequest
ที่ใช้รอบการทำงาน
จากสถานะหนึ่งไปยังอีกสถานะหนึ่ง
รายการต่อไปนี้อธิบายขั้นตอนของวงจร
UrlRequest
- วงจรนี้จะอยู่ในสถานะเริ่มแล้วหลังจากที่แอปเรียกใช้เมธอด
start()
- เซิร์ฟเวอร์อาจส่งการตอบกลับการเปลี่ยนเส้นทาง ซึ่งจะนำโฟลว์ไปยังเมธอด
onRedirectReceived()
ในวิธีนี้ คุณสามารถดำเนินการอย่างใดอย่างหนึ่งต่อไปนี้ในฝั่งไคลเอ็นต์
- ติดตามการเปลี่ยนเส้นทางโดยใช้
followRedirect()
วิธีนี้จะเปลี่ยนคำขอกลับไปเป็นสถานะเริ่มแล้ว
- ยกเลิกคำขอโดยใช้
cancel()
วิธีนี้จะนำคำขอไปยังเมธอด
onCanceled()
ซึ่งแอปสามารถดำเนินการเพิ่มเติมได้ก่อนที่จะย้ายคำขอ
ไปยังสถานะสุดท้ายที่ยกเลิกแล้ว
- หลังจากที่แอปทำตามการเปลี่ยนเส้นทางทั้งหมดแล้ว เซิร์ฟเวอร์จะส่งส่วนหัวการตอบกลับและเรียกใช้เมธอด
onResponseStarted()
คำขออยู่ในสถานะรอ read() แอปควรเรียกใช้เมธอด read()
เพื่อพยายามอ่านส่วนหนึ่งของเนื้อหาการตอบกลับ หลังจากเรียกใช้ read()
แล้ว คำขอจะอยู่ในสถานะกำลังอ่าน ซึ่งมีผลลัพธ์ที่เป็นไปได้ดังนี้
- การดำเนินการอ่านสำเร็จแล้ว แต่ยังมีข้อมูลเพิ่มเติม ระบบจะเรียกใช้
onReadCompleted()
และคำขอจะกลับไปอยู่ในสถานะรอ read() อีกครั้ง
แอปควรเรียกใช้เมธอด
read()
อีกครั้งเพื่ออ่านเนื้อหาการตอบกลับต่อไป นอกจากนี้ แอปยังหยุดอ่านคำขอได้โดยใช้เมธอด cancel()
- การดำเนินการอ่านสำเร็จแล้ว และไม่มีข้อมูลเพิ่มเติม
ระบบจะเรียกใช้เมธอด
onSucceeded()
และคำขอจะอยู่ในสถานะสุดท้ายเป็นสำเร็จ
- การดำเนินการอ่านไม่สำเร็จ ระบบจะเรียกใช้เมธอด
onFailed
และสถานะสุดท้ายของคำขอจะเป็นไม่สำเร็จ
แผนภาพต่อไปนี้แสดงวงจรของออบเจ็กต์ UrlRequest

วงจรชีวิตของคำขอ Cronet
Legend |
|
สถานะเริ่มต้น |
สถานะสุดท้าย |
สถานะชั่วคราว |
วิธีการเรียกกลับ |
UrlRequest วิธี |
|
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 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 | |"]]