UrlRequest.Builder


public static abstract class UrlRequest.Builder
extends Object

java.lang.Object
   ↳ android.net.http.UrlRequest.Builder


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

Summary

Public methods

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

Adds a request header.

abstract UrlRequest.Builder bindToNetwork(Network network)

Binds the request to the specified network.

abstract UrlRequest build()

Creates a UrlRequest using configuration within this Builder.

abstract UrlRequest.Builder setCacheDisabled(boolean disableCache)

Whether to disable cache for the request.

abstract UrlRequest.Builder setDirectExecutorAllowed(boolean allowDirectExecutor)

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

abstract UrlRequest.Builder setHttpMethod(String method)

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.

abstract UrlRequest.Builder setTrafficStatsTag(int tag)

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

abstract UrlRequest.Builder setTrafficStatsUid(int uid)

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.

Inherited methods

Public methods

addHeader

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder addHeader (String header, 
                String value)

Adds a request header.

Parameters
header String: header name. This value cannot be null.

value String: header value. This value cannot be null.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

bindToNetwork

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder bindToNetwork (Network network)

Binds the request to the specified network. The HTTP stack 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.

Parameters
network Network: the network to bind the request to. Specify null to unbind.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

build

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest build ()

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

Returns
UrlRequest constructed UrlRequest using configuration within this Builder. This value cannot be null.

setCacheDisabled

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder setCacheDisabled (boolean disableCache)

Whether to disable cache for the request. If the engine is not set up to use cache, this call has no effect.

Parameters
disableCache boolean: true to disable cache, false otherwise.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setDirectExecutorAllowed

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder setDirectExecutorAllowed (boolean allowDirectExecutor)

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

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

Parameters
allowDirectExecutor boolean: true to allow executors performing inline execution, false otherwise.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setHttpMethod

Added in API level 34
Also in S Extensions 7
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
method String: "GET", "HEAD", "DELETE", "POST" or "PUT". This value cannot be null.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setPriority

Added in API level 34
Also in S Extensions 7
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 UrlRequest.REQUEST_PRIORITY_MEDIUM priority if this method is not called.

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

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setTrafficStatsTag

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder setTrafficStatsTag (int tag)

Sets TrafficStats tag to use when accounting socket traffic caused by this request. See 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
tag int: 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 DownloadManager when performing traffic on behalf of an application.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setTrafficStatsUid

Added in API level 34
Also in S Extensions 7
public abstract UrlRequest.Builder setTrafficStatsUid (int uid)

Sets specific UID to use when accounting socket traffic caused by this request. See TrafficStats for more information. Designed for use when performing an operation on behalf of another application. Caller must hold android.Manifest.permission#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
uid int: the UID to attribute socket traffic caused by this request.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.

setUploadDataProvider

Added in API level 34
Also in S Extensions 7
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. This value cannot be null.

executor Executor: All uploadDataProvider methods will be invoked using this Executor. May optionally be the same Executor the request itself is using. This value cannot be null.

Returns
UrlRequest.Builder the builder to facilitate chaining. This value cannot be null.