Cronet isteği yaşam döngüsü
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
Cronet kullanılarak oluşturulan isteklerin yaşam döngüsü ve kitaplık tarafından sağlanan geri çağırma yöntemlerini kullanarak bu istekleri nasıl yöneteceğinizi öğrenin.
Yaşam döngüsüne genel bakış
Cronet Kitaplığı kullanılarak oluşturulan ağ istekleri UrlRequest
sınıfıyla temsil edilir.
UrlRequest
yaşam döngüsünü anlamak için aşağıdaki kavramlar önemlidir:
- Eyaletler
- Eyalet, isteğin belirli bir zamanda bulunması koşuludur.
Cronet Kitaplığı kullanılarak oluşturulan UrlRequest nesneleri yaşam döngülerinde farklı durumlardan geçer. İstek yaşam döngüsü, bir başlangıç durumunun yanı sıra birden fazla geçiş ve nihai durumu içerir.
UrlRequest
yöntem
- İstemciler, duruma bağlı olarak
UrlRequest
nesnelerinde belirli yöntemleri çağırabilir. Yöntemler, isteği bir durumdan diğerine taşır.
Callback
yöntem
UrlRequest.Callback
sınıfının yöntemlerini uyguladığınızda uygulamanız isteğin ilerleme durumuyla ilgili güncellemeleri alabilir. Yaşam döngüsünü bir durumdan diğerine geçiren UrlRequest
nesnesinin yöntemlerini çağırmak için geri çağırma yöntemlerini uygulayabilirsiniz.
Aşağıdaki listede UrlRequest
yaşam döngüsünün akışı açıklanmaktadır:
- Yaşam döngüsü, uygulamanızın
start()
yöntemini çağırdıktan sonra Başlatıldı durumundadır.
- Sunucu, akışı
onRedirectReceived()
yöntemine taşıyan bir yönlendirme yanıtı gönderebilir. Bu yöntemde, aşağıdaki istemci işlemlerinden birini yapabilirsiniz:
followRedirect()
kullanarak yönlendirmeyi takip edin.
Bu yöntem, isteği Başlatıldı durumuna geri getirir.
- İsteği
cancel()
kullanarak iptal edin.
Bu yöntem, istek İptal edildi son durumuna taşınmadan önce uygulamanın ek işlemler gerçekleştirebileceği onCanceled()
yöntemine alır.
- Uygulama tüm yönlendirmeleri izledikten sonra, sunucu yanıt üst bilgilerini gönderir ve
onResponseStarted()
yöntemi çağrılır. İstek, Read() bekleniyor durumundadır. Uygulama, yanıt gövdesinin bir kısmını okumaya çalışmak için read()
yöntemini çağırmalıdır. read()
çağrıldıktan sonra, istek Okundu durumunda olur. Bu durumda aşağıdaki olası sonuçlar mevcuttur:
- Okuma işlemi başarılı oldu, ancak daha fazla veri mevcut.
onReadCompleted()
çağrılır ve istek tekrar Read() bekleniyor durumunda olur.
Uygulama, yanıt gövdesini okumaya devam etmek için read()
yöntemini tekrar çağırmalıdır. Uygulama, cancel()
yöntemini kullanarak isteği okumayı da durdurabilir .
- Okuma işlemi başarılı oldu ve başka veri yok.
onSucceeded()
yöntemi çağrılır ve istek artık Başarılı son durumunda olur.
- Okuma işlemi başarısız oldu.
onFailed
yöntemi çağrılır ve isteğin son durumu artık Failed (Başarısız) olur.
Aşağıdaki şemada bir UrlRequest
nesnesinin yaşam döngüsü gösterilmektedir:

Cronet isteğinin yaşam döngüsü
Açıklama |
|
başlangıç durumu |
son durum |
geçiş durumu |
geri çağırma yöntemi |
UrlRequest yöntem |
|
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-07-27 UTC.
[null,null,["Son güncelleme tarihi: 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 | |"]]