ImplicitFlowControlCallback

public abstract class ImplicitFlowControlCallback extends UrlRequest.Callback

Known direct subclasses
InMemoryTransformCronetCallback

An abstract Cronet callback that reads the entire body to memory and optionally deserializes the body before passing it back to the issuer of the HTTP request.

Known indirect subclasses
ByteArrayCronetCallback

A specialization of InMemoryTransformCronetCallback which returns the body bytes verbatim without any interpretation.

JsonCronetCallback

A specialization of InMemoryTransformCronetCallback that interprets the response body as JSON.

StringCronetCallback

A specialization of InMemoryTransformCronetCallback that interprets the response body as a string.


An implementation of UrlRequest.Callback that takes away the difficulty of managing the request lifecycle away, and automatically proceeds to read the response entirely.

Summary

Public constructors

Public methods

final void

Invoked if request was canceled via cancel.

final void
onFailed(
    UrlRequest request,
    UrlResponseInfo info,
    CronetException error
)

Invoked if request failed for any reason after start.

final void
onReadCompleted(
    UrlRequest request,
    UrlResponseInfo info,
    ByteBuffer byteBuffer
)

Invoked whenever part of the response body has been read.

final void
onRedirectReceived(
    UrlRequest request,
    UrlResponseInfo info,
    String newLocationUrl
)

Invoked whenever a redirect is encountered.

final void

Invoked when the final set of headers, after all redirects, is received.

final void

Invoked when request is completed successfully.

Protected methods

abstract void

Invoked whenever part of the response body has been read.

abstract void

Invoked if request was canceled via cancel.

abstract void

Invoked if request failed for any reason after start.

abstract void

Invoked when the final set of headers, after all redirects, is received.

abstract void

Invoked when request is completed successfully.

abstract boolean

Invoked whenever a redirect is encountered.

Public constructors

ImplicitFlowControlCallback

public ImplicitFlowControlCallback()

Public methods

onCanceled

public final void onCanceled(UrlRequest request, UrlResponseInfo info)

Invoked if request was canceled via cancel. Once invoked, no other Callback methods will be invoked. Default implementation takes no action.

Parameters
UrlRequest request

Request that was canceled.

UrlResponseInfo info

Response information. May be null if no response was received.

onFailed

public final void onFailed(
    UrlRequest request,
    UrlResponseInfo info,
    CronetException error
)

Invoked if request failed for any reason after start. Once invoked, no other Callback methods will be invoked. error provides information about the failure.

Parameters
UrlRequest request

Request that failed.

UrlResponseInfo info

Response information. May be null if no response was received.

CronetException error

information about error.

onReadCompleted

public final 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 onCanceled(), no other Callback method will be invoked for the request, including onSucceeded() and onFailed(), until UrlRequest.read() is called to attempt to continue reading the response body.

Parameters
UrlRequest request

Request that received data.

UrlResponseInfo info

Response information.

ByteBuffer byteBuffer

The buffer that was passed in to UrlRequest.read(), 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.

Throws
java.lang.Exception

if an error occurs while processing a read completion. onFailed will be called with the thrown exception set as the cause of the CallbackException.

onRedirectReceived

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

Invoked whenever a redirect is encountered. This will only be invoked between the call to start and onResponseStarted(). The body of the redirect response, if it has one, will be ignored. The redirect will not be followed until the URLRequest's followRedirect method is called, either synchronously or asynchronously.

Parameters
UrlRequest request

Request being redirected.

UrlResponseInfo info

Response information.

String newLocationUrl

Location where request is redirected.

Throws
java.lang.Exception

if an error occurs while processing a redirect. onFailed will be called with the thrown exception set as the cause of the CallbackException.

onResponseStarted

public final 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 onCanceled(), no other Callback method will be invoked for the request, including onSucceeded() and onFailed(), until UrlRequest.read() is called to attempt to start reading the response body.

Parameters
UrlRequest request

Request that started to get response.

UrlResponseInfo info

Response information.

Throws
java.lang.Exception

if an error occurs while processing response start. onFailed will be called with the thrown exception set as the cause of the CallbackException.

onSucceeded

public final void onSucceeded(UrlRequest request, UrlResponseInfo info)

Invoked when request is completed successfully. Once invoked, no other Callback methods will be invoked.

Parameters
UrlRequest request

Request that succeeded.

UrlResponseInfo info

Response information.

Protected methods

onBodyChunkRead

protected abstract void onBodyChunkRead(UrlResponseInfo info, ByteBuffer bodyChunk)

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. The buffer is ready for reading. Buffers are reused internally so the implementing class shouldn't store the buffer or use it anywhere else than in the implementation of this method.

Parameters
UrlResponseInfo info

Response information.

ByteBuffer bodyChunk

The buffer that contains the received data, flipped for reading.

Throws
java.lang.Exception

if an error occurs while processing a read completion. onFailed will be called with the thrown exception set as the cause of the CallbackException.

onCanceled

protected abstract void onCanceled(@Nullable UrlResponseInfo info)

Invoked if request was canceled via cancel. Once invoked, no other UrlRequest.Callback methods will be invoked.

Parameters
@Nullable UrlResponseInfo info

Response information. May be null if no response was received.

onFailed

protected abstract void onFailed(@Nullable UrlResponseInfo info, CronetException exception)

Invoked if request failed for any reason after start. Once invoked, no other UrlRequest.Callback methods will be invoked. error provides information about the failure.

Parameters
@Nullable UrlResponseInfo info

Response information. May be null if no response was received.

CronetException exception

information about error.

onResponseStarted

protected abstract void onResponseStarted(UrlResponseInfo info)

Invoked when the final set of headers, after all redirects, is received. Will only be invoked once for each request. It's guaranteed that Cronet doesn't start reading the body until this method returns.

Parameters
UrlResponseInfo info

Response information.

Throws
java.lang.Exception

if an error occurs while processing response start. onFailed will be called with the thrown exception set as the cause of the CallbackException.

onSucceeded

protected abstract void onSucceeded(UrlResponseInfo info)

Invoked when request is completed successfully. Once invoked, no other UrlRequest.Callback methods will be invoked.

Parameters
UrlResponseInfo info

Response information.

shouldFollowRedirect

protected abstract boolean shouldFollowRedirect(UrlResponseInfo info, String newLocationUrl)

Invoked whenever a redirect is encountered. This will only be invoked between the call to start and onResponseStarted(). The body of the redirect response, if it has one, will be ignored.

Parameters
UrlResponseInfo info

Response information.

String newLocationUrl

Location where request is redirected.

Returns
boolean

true if the redirect should be followed, false if the request should be canceled.

Throws
java.lang.Exception

if an error occurs while processing a redirect. onFailed will be called with the thrown exception set as the cause of the CallbackException.