Siklus proses permintaan Cronet
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pelajari siklus proses permintaan yang dibuat menggunakan Cronet dan cara mengelolanya
menggunakan metode callback yang disediakan oleh library.
Ringkasan siklus proses
Permintaan jaringan yang dibuat menggunakan Library Cronet direpresentasikan oleh
class UrlRequest
.
Konsep berikut penting untuk memahami
siklus proses UrlRequest
:
- Status
- Status adalah kondisi khusus yang dimiliki permintaan pada waktu tertentu.
Objek UrlRequest yang dibuat menggunakan Library
Cronet bergerak melalui berbagai status dalam siklus prosesnya. Siklus proses
permintaan mencakup status awal, serta beberapa status transisi dan
final.
- Metode
UrlRequest
- Klien dapat memanggil metode tertentu pada
objek
UrlRequest
bergantung pada
statusnya. Metode tersebut memindahkan permintaan dari satu status ke status lainnya.
- Metode
Callback
- Dengan mengimplementasikan metode
class
UrlRequest.Callback
, aplikasi
Anda dapat menerima info terbaru tentang progres permintaan. Anda dapat mengimplementasikan
metode callback untuk memanggil metode
objek UrlRequest
yang mengambil siklus proses
dari suatu status ke status lainnya.
Daftar berikut menjelaskan alur
siklus proses UrlRequest
:
- Siklus proses berada dalam status Dimulai setelah aplikasi Anda memanggil
metode
start()
.
- Server dapat mengirim respons pengalihan, yang membawa alur ke
metode
onRedirectReceived()
. Dalam metode ini, Anda dapat melakukan salah satu tindakan klien berikut:
- Ikuti pengalihan menggunakan
followRedirect()
.
Metode ini mengembalikan permintaan ke status Dimulai.
- Batalkan permintaan menggunakan
cancel()
.
Metode ini membawa permintaan ke metode
onCanceled()
tempat aplikasi dapat melakukan operasi tambahan sebelum permintaan
dipindahkan ke status Dibatalkan terakhir.
- Setelah aplikasi mengikuti semua pengalihan, server akan mengirimkan header respons dan metode
onResponseStarted()
dipanggil. Permintaan berada dalam status Menunggu dibaca(). Aplikasi
harus memanggil metode read()
untuk mencoba membaca bagian dari isi respons. Setelah read()
dipanggil, permintaan berada dalam status Reading, yang terdapat
kemungkinan hasil berikut:
- Tindakan membaca berhasil, tetapi ada data lain yang tersedia.
onReadCompleted()
dipanggil dan permintaan berada dalam status Menunggu read() lagi.
Aplikasi harus memanggil metode
read()
lagi untuk melanjutkan membaca isi respons. Aplikasi juga
dapat berhenti membaca permintaan menggunakan
metode cancel()
.
- Tindakan membaca berhasil, dan tidak ada data lain yang tersedia.
Metode
onSucceeded()
dipanggil dan permintaan sekarang berada dalam status Berhasil
terakhir.
- Tindakan membaca gagal. Metode
onFailed
dipanggil dan status akhir permintaan sekarang menjadi Gagal.
Diagram berikut menunjukkan siklus proses
objek UrlRequest
:

Siklus proses permintaan Cronet
Keterangan |
|
Status awal  |
Status akhir  |
Status transisi  |
Metode callback  |
Metode UrlRequest |
|
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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 | |"]]