# UrlRequest.Callback

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)

# Callback

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/net/http/UrlRequest.Callback "View this page in Java")  

```
interface Callback
```

|------------------------------------------------------------------------------------------------------------------------------|
| [android.net.http.UrlRequest.Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#) |

Users of the HTTP stack extend this class to receive callbacks indicating the progress of a[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)being processed. An instance of this class is passed in to[UrlRequest.Builder](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Builder)'s constructor when constructing the`UrlRequest`.

Note: All methods will be invoked on the thread of the[java.util.concurrent.Executor](https://developer.android.com/reference/kotlin/java/util/concurrent/Executor.html#)used during construction of the`UrlRequest`.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                       Public methods                                                                                                                                                                                                                                                                                                                                                                                       ||
|--------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onCanceled](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onCanceled(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)?`)` Invoked if request was canceled via[UrlRequest.cancel](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#cancel()).                                                                                                                                                   |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onFailed](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)?`, `error:` `[HttpException](https://developer.android.com/reference/kotlin/android/net/http/HttpException)`)` Invoked if request failed for any reason after[UrlRequest.start](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#start()). |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onReadCompleted](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onReadCompleted(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20java.nio.ByteBuffer))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)`, `byteBuffer:` `[ByteBuffer](https://developer.android.com/reference/kotlin/java/nio/ByteBuffer.html#)`)` Invoked whenever part of the response body has been read.                                                                                              |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onRedirectReceived](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onRedirectReceived(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20kotlin.String))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)`, `newLocationUrl:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Invoked whenever a redirect is encountered.                                                                                                              |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onResponseStarted](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onResponseStarted(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)`)` Invoked when the final set of headers, after all redirects, is received.                                                                                                                                                                                                          |
| abstract[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [onSucceeded](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onSucceeded(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo))`(`request:` `[UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest)`, `info:` `[UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)`)` Invoked when request is completed successfully.                                                                                                                                                                                                                                               |

## Public methods

### onCanceled

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onCanceled(
    request: UrlRequest, 
    info: UrlResponseInfo?
): Unit
```

Invoked if request was canceled via[UrlRequest.cancel](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#cancel()). Once invoked, no other[Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#)methods will be invoked. Default implementation takes no action.

|                                                                                                                                                                                                            Parameters                                                                                                                                                                                                            ||
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request` | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request that was canceled.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`    | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)?:Response information. May be`null`if no response was received.                                                                                                                                                                                                                                                    |

### onFailed

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onFailed(
    request: UrlRequest, 
    info: UrlResponseInfo?, 
    error: HttpException
): Unit
```

Invoked if request failed for any reason after[UrlRequest.start](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#start()). Once invoked, no other[Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#)methods will be invoked.`error`provides information about the failure.

|                                                                                                                                                                                                         Parameters                                                                                                                                                                                                         ||
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request` | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request that failed.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`    | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo)?:Response information. May be`null`if no response was received.                                                                                                                                                                                                                                              |
| `error`   | [HttpException](https://developer.android.com/reference/kotlin/android/net/http/HttpException):information about error. This value cannot be`null`.                                                                                                                                                                                                                                                             |

### onReadCompleted

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onReadCompleted(
    request: UrlRequest, 
    info: UrlResponseInfo, 
    byteBuffer: ByteBuffer
): Unit
```

Invoked whenever part of the response body has been read. Only part of the buffer may be populated, even if the entire response body has not yet been consumed. With the exception of[onCanceled()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onCanceled(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)), no other[Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#)method will be invoked for the request, including[onSucceeded()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onSucceeded(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo))and[onFailed()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)), until[UrlRequest.read()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#read(java.nio.ByteBuffer))is called to attempt to continue reading the response body.

|                                                                                                                                                                                                              Parameters                                                                                                                                                                                                              ||
|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request`    | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request that received data.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`       | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo):Response information. This value cannot be`null`.                                                                                                                                                                                                                                                                   |
| `byteBuffer` | [ByteBuffer](https://developer.android.com/reference/kotlin/java/nio/ByteBuffer.html#):The buffer that was passed in to[UrlRequest.read()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#read(java.nio.ByteBuffer)), now containing the received data. The buffer's position is updated to the end of the received data. The buffer's limit is not changed. This value cannot be`null`.   |

|                                                                                                                                                                                                                           Exceptions                                                                                                                                                                                                                            ||
|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.Exception` | if an error occurs while processing a read completion.[onFailed](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException))will be called with the thrown exception set as the cause of the[CallbackException](https://developer.android.com/reference/kotlin/android/net/http/CallbackException). |

### onRedirectReceived

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onRedirectReceived(
    request: UrlRequest, 
    info: UrlResponseInfo, 
    newLocationUrl: String
): Unit
```

Invoked whenever a redirect is encountered. This will only be invoked between the call to[UrlRequest.start](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#start())and[onResponseStarted()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onResponseStarted(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)). The body of the redirect response, if it has one, will be ignored. The redirect will not be followed until the URLRequest's[android.net.http.UrlRequest#followRedirect](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#followRedirect())method is called, either synchronously or asynchronously.

|                                                                                                                                                                                                               Parameters                                                                                                                                                                                                               ||
|------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request`        | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request being redirected.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`           | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo):Response information. This value cannot be`null`.                                                                                                                                                                                                                                                                 |
| `newLocationUrl` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):Location where request is redirected. This value cannot be`null`.                                                                                                                                                                                                                                                                   |

|                                                                                                                                                                                                                        Exceptions                                                                                                                                                                                                                        ||
|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.Exception` | if an error occurs while processing a redirect.[onFailed](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException))will be called with the thrown exception set as the cause of the[CallbackException](https://developer.android.com/reference/kotlin/android/net/http/CallbackException). |

### onResponseStarted

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onResponseStarted(
    request: UrlRequest, 
    info: UrlResponseInfo
): Unit
```

Invoked when the final set of headers, after all redirects, is received. Will only be invoked once for each request. With the exception of[onCanceled()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onCanceled(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)), no other[Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#)method will be invoked for the request, including[onSucceeded()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onSucceeded(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo))and[onFailed()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)), until[UrlRequest.read()](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest#read(java.nio.ByteBuffer))is called to attempt to start reading the response body.

|                                                                                                                                                                                                                 Parameters                                                                                                                                                                                                                  ||
|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request` | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request that started to get response.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`    | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo):Response information. This value cannot be`null`.                                                                                                                                                                                                                                                                             |

|                                                                                                                                                                                                                          Exceptions                                                                                                                                                                                                                          ||
|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.Exception` | if an error occurs while processing response start.[onFailed](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException))will be called with the thrown exception set as the cause of the[CallbackException](https://developer.android.com/reference/kotlin/android/net/http/CallbackException). |

### onSucceeded

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Also in[S Extensions 7](https://developer.android.com/sdkExtensions)  

```
abstract fun onSucceeded(
    request: UrlRequest, 
    info: UrlResponseInfo
): Unit
```

Invoked when request is completed successfully. Once invoked, no other[Callback](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest.Callback#)methods will be invoked.

|                                                                                                                                                                                                          Parameters                                                                                                                                                                                                           ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `request` | [UrlRequest](https://developer.android.com/reference/kotlin/android/net/http/UrlRequest):Request that succeeded.**This is not guaranteed to be the same object as the one received by other callbacks, nor is it guaranteed to be the one returned by URLRequest.Builder#build.** However, method calls on this object will have the same effects as calls on the original URLRequest. This value cannot be`null`. |
| `info`    | [UrlResponseInfo](https://developer.android.com/reference/kotlin/android/net/http/UrlResponseInfo):Response information. This value cannot be`null`.                                                                                                                                                                                                                                                               |