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

# UrlRequest.Callback

*** ** * ** ***


`
public
static


interface
UrlRequest.Callback
`


`


`

|---|
| android.net.http.UrlRequest.Callback |

<br />

*** ** * ** ***

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


Note: All methods will be invoked on the thread of the `https://developer.android.com/reference/java/util/concurrent/Executor`
used during construction of the `UrlRequest`.

## Summary

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

## Public methods

### onCanceled

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

```
public abstract void onCanceled (UrlRequest request, 
                UrlResponseInfo info)
```

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

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. May be `null` if no response was received. <br /> |

### onFailed

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

```
public abstract void onFailed (UrlRequest request, 
                UrlResponseInfo info, 
                HttpException error)
```

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

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. May be `null` if no response was received. <br /> |
| `error` | `HttpException`: information about error. This value cannot be `null`. <br /> |

### onReadCompleted

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

```
public abstract void onReadCompleted (UrlRequest request, 
                UrlResponseInfo info, 
                ByteBuffer byteBuffer)
```

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 `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onCanceled(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)`, no other `https://developer.android.com/reference/android/net/http/UrlRequest.Callback`
method will be invoked for the request, including `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onSucceeded(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)` and `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)`, until `https://developer.android.com/reference/android/net/http/UrlRequest#read(java.nio.ByteBuffer)` is called to attempt to continue reading the response body.

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. This value cannot be `null`. <br /> |
| `byteBuffer` | `ByteBuffer`: The buffer that was passed in to `https://developer.android.com/reference/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`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` | if an error occurs while processing a read completion. `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/net/http/CallbackException`. |

### onRedirectReceived

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

```
public abstract void onRedirectReceived (UrlRequest request, 
                UrlResponseInfo info, 
                String newLocationUrl)
```

Invoked whenever a redirect is encountered. This will only be invoked between the call to
`https://developer.android.com/reference/android/net/http/UrlRequest#start()` and `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/net/http/UrlRequest#followRedirect()` method is called, either synchronously or asynchronously.

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. This value cannot be `null`. <br /> |
| `newLocationUrl` | `String`: Location where request is redirected. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` | if an error occurs while processing a redirect. `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/net/http/CallbackException`. |

### onResponseStarted

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

```
public abstract void onResponseStarted (UrlRequest request, 
                UrlResponseInfo info)
```

Invoked when the final set of headers, after all redirects, is received. Will only be
invoked once for each request.

With the exception of `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onCanceled(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)`, no other `https://developer.android.com/reference/android/net/http/UrlRequest.Callback`
method will be invoked for the request, including `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onSucceeded(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo)` and `https://developer.android.com/reference/android/net/http/UrlRequest.Callback#onFailed(android.net.http.UrlRequest,%20android.net.http.UrlResponseInfo,%20android.net.http.HttpException)`, until `https://developer.android.com/reference/android/net/http/UrlRequest#read(java.nio.ByteBuffer)` is called to attempt to start reading the response body.

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` | if an error occurs while processing response start. `https://developer.android.com/reference/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 `https://developer.android.com/reference/android/net/http/CallbackException`. |

### onSucceeded

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

```
public abstract void onSucceeded (UrlRequest request, 
                UrlResponseInfo info)
```

Invoked when request is completed successfully. Once invoked, no other `https://developer.android.com/reference/android/net/http/UrlRequest.Callback`
methods will be invoked.

<br />

| Parameters ||
|---|---|
| `request` | `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 `https://developer.android.com/`.** However, method calls on this object will have the same effects as calls on the original `https://developer.android.com/`. This value cannot be `null`. <br /> |
| `info` | `UrlResponseInfo`: Response information. This value cannot be `null`. <br /> |