UrlRequest.Builder

public abstract class UrlRequest.Builder


Builder for UrlRequests. Allows configuring requests before constructing them with build. The builder can be created by calling newUrlRequestBuilder.

Summary

Constants

static final int

Highest request priority.

static final int

Lowest request priority.

static final int

Low request priority.

static final int

Very low request priority.

static final int

Medium request priority.

Public constructors

Public methods

abstract UrlRequest.Builder
addHeader(String header, String value)

Adds a request header.

UrlRequest.Builder

Associates the annotation object with this request.

abstract UrlRequest.Builder

Marks that the executors this request will use to notify callbacks (for UploadDataProviders and UrlRequest.Callbacks) is intentionally performing inline execution, like Guava's directExecutor or java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy.

UrlRequest.Builder
bindToNetwork(long networkHandle)

Binds the request to the specified network handle.

abstract UrlRequest

Creates a UrlRequest using configuration within this Builder.

abstract UrlRequest.Builder

Disables cache for the request.

abstract UrlRequest.Builder

Sets the HTTP method verb to use for this request.

abstract UrlRequest.Builder
setPriority(int priority)

Sets priority of the request which should be one of the REQUEST_PRIORITY_* values.

UrlRequest.Builder
@UrlRequest.Experimental
setRawCompressionDictionary(
    byte[] dictionarySha256Hash,
    ByteBuffer dictionary,
    String dictionaryId
)

Allows Cronet to use the specified compression dictionary for this request.

UrlRequest.Builder

Sets a listener that gets invoked after onCanceled(), onFailed() or onSucceeded() return.

UrlRequest.Builder

Sets android.net.TrafficStats tag to use when accounting socket traffic caused by this request.

UrlRequest.Builder

Sets specific UID to use when accounting socket traffic caused by this request.

abstract UrlRequest.Builder
setUploadDataProvider(
    UploadDataProvider uploadDataProvider,
    Executor executor
)

Sets upload data provider.

Constants

REQUEST_PRIORITY_HIGHEST

public static final int REQUEST_PRIORITY_HIGHEST = 4

Highest request priority. Passed to setPriority.

REQUEST_PRIORITY_IDLE

public static final int REQUEST_PRIORITY_IDLE = 0

Lowest request priority. Passed to setPriority.

REQUEST_PRIORITY_LOW

public static final int REQUEST_PRIORITY_LOW = 2

Low request priority. Passed to setPriority.

REQUEST_PRIORITY_LOWEST

public static final int REQUEST_PRIORITY_LOWEST = 1

Very low request priority. Passed to setPriority.

REQUEST_PRIORITY_MEDIUM

public static final int REQUEST_PRIORITY_MEDIUM = 3

Medium request priority. Passed to setPriority. This is the default priority given to the request.

Public constructors

Builder

public Builder()

Public methods

addHeader

public abstract UrlRequest.Builder addHeader(String header, String value)

Adds a request header.

Parameters
String header

header name.

String value

header value.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

addRequestAnnotation

public UrlRequest.Builder addRequestAnnotation(Object annotation)

Associates the annotation object with this request. May add more than one. Passed through to a RequestFinishedInfo.Listener, see getAnnotations.

Parameters
Object annotation

an object to pass on to the RequestFinishedInfo.Listener with a RequestFinishedInfo.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

allowDirectExecutor

public abstract UrlRequest.Builder allowDirectExecutor()

Marks that the executors this request will use to notify callbacks (for UploadDataProviders and UrlRequest.Callbacks) is intentionally performing inline execution, like Guava's directExecutor or java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy.

Warning: This option makes it easy to accidentally block the network thread. It should not be used if your callbacks perform disk I/O, acquire locks, or call into other code you don't carefully control and audit.

bindToNetwork

public UrlRequest.Builder bindToNetwork(long networkHandle)

Binds the request to the specified network handle. Cronet will send this request only using the network associated to this handle. If this network disconnects the request will fail, the exact error will depend on the stage of request processing when the network disconnects. Network handles can be obtained through Network#getNetworkHandle. Only available starting from Android Marshmallow.

Parameters
long networkHandle

the network handle to bind the request to. Specify UNBIND_NETWORK_HANDLE to unbind.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

build

public abstract UrlRequest build()

Creates a UrlRequest using configuration within this Builder. The returned UrlRequest can then be started by calling start.

Returns
UrlRequest

constructed UrlRequest using configuration within this Builder.

disableCache

public abstract UrlRequest.Builder disableCache()

Disables cache for the request. If context is not set up to use cache, this call has no effect.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setHttpMethod

public abstract UrlRequest.Builder setHttpMethod(String method)

Sets the HTTP method verb to use for this request.

The default when this method is not called is "GET" if the request has no body or "POST" if it does.

Parameters
String method

"GET", "HEAD", "DELETE", "POST" or "PUT".

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setPriority

public abstract UrlRequest.Builder setPriority(int priority)

Sets priority of the request which should be one of the REQUEST_PRIORITY_* values. The request is given REQUEST_PRIORITY_MEDIUM priority if this method is not called.

Parameters
int priority

priority of the request which should be one of the REQUEST_PRIORITY_* values.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setRawCompressionDictionary

@UrlRequest.Experimental
public UrlRequest.Builder setRawCompressionDictionary(
    byte[] dictionarySha256Hash,
    ByteBuffer dictionary,
    String dictionaryId
)

Allows Cronet to use the specified compression dictionary for this request. When specified, Cronet might signal to the server the availability of said compression dictionary. For this to have any effect, the CronetEngine that will execute this request must have been configured to enable a compression scheme that supports external dictionaries. This partially implements draft-ietf-httpbis-compression-dictionary within Cronet. Cronet won't directly handle "Use-As-Dictionary" response headers, it will instead rely on the embedder to: either, handle them and later call UrlRequest.Builder#setCompressionDictionary for requests which match Use-As-Dictionary's rules; or, fetch compression dictionaries via some other out of band mechanism, and later call UrlRequest.Builder#setCompressionDictionary. Cronet will interpret the dictionary as matching only this UrlRequest. If said request is redirected, and the embedder decides to follow the redirect, the dictionary will match also the new URL.

Parameters
byte[] dictionarySha256Hash

the SHA-256 of the specified compression dictionary.

ByteBuffer dictionary

the compression dictionary that Cronet can use for this UrlRequest. This must be a direct ByteBuffer.

String dictionaryId

the optional ID associated with this dictionary, must be an empty string if missing. If present, this will be sent via the Dictionary-ID header. You would need to specify this if the server specified an id in its "Use-As-Dictionary" response.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setRequestFinishedListener

public UrlRequest.Builder setRequestFinishedListener(RequestFinishedInfo.Listener listener)

Sets a listener that gets invoked after onCanceled(), onFailed() or onSucceeded() return.

The listener is invoked with the request finished info on an java.util.concurrent.Executor provided by getExecutor().

Parameters
RequestFinishedInfo.Listener listener

the listener for finished requests.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setTrafficStatsTag

public UrlRequest.Builder setTrafficStatsTag(int tag)

Sets android.net.TrafficStats tag to use when accounting socket traffic caused by this request. See android.net.TrafficStats for more information. If no tag is set (e.g. this method isn't called), then Android accounts for the socket traffic caused by this request as if the tag value were set to 0.

NOTE:Setting a tag disallows sharing of sockets with requests with other tags, which may adversely effect performance by prohibiting connection sharing. In other words use of multiplexed sockets (e.g. HTTP/2 and QUIC) will only be allowed if all requests have the same socket tag.

Parameters
int tag

the tag value used to when accounting for socket traffic caused by this request. Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services like android.app.DownloadManager when performing traffic on behalf of an application.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setTrafficStatsUid

public UrlRequest.Builder setTrafficStatsUid(int uid)

Sets specific UID to use when accounting socket traffic caused by this request. See android.net.TrafficStats for more information. Designed for use when performing an operation on behalf of another application. Caller must hold MODIFY_NETWORK_ACCOUNTING permission. By default traffic is attributed to UID of caller.

NOTE:Setting a UID disallows sharing of sockets with requests with other UIDs, which may adversely effect performance by prohibiting connection sharing. In other words use of multiplexed sockets (e.g. HTTP/2 and QUIC) will only be allowed if all requests have the same UID set.

Parameters
int uid

the UID to attribute socket traffic caused by this request.

Returns
UrlRequest.Builder

the builder to facilitate chaining.

setUploadDataProvider

public abstract UrlRequest.Builder setUploadDataProvider(
    UploadDataProvider uploadDataProvider,
    Executor executor
)

Sets upload data provider. Switches method to "POST" if not explicitly set. Starting the request will throw an exception if a Content-Type header is not set.

Parameters
UploadDataProvider uploadDataProvider

responsible for providing the upload data.

Executor executor

All uploadDataProvider methods will be invoked using this Executor. May optionally be the same Executor the request itself is using.

Returns
UrlRequest.Builder

the builder to facilitate chaining.