# Print output for @column tags ?> NetworkRequest.Builder - Android SDK | Android Developers

Most visited

Recently visited

NetworkRequest.Builder

public static class NetworkRequest.Builder
extends Object

java.lang.Object
   ↳ android.net.NetworkRequest.Builder


Builder used to create NetworkRequest objects. Specify the Network features needed in terms of NetworkCapabilities features

Summary

Public constructors

Builder()

Default constructor for Builder.

Public methods

NetworkRequest.Builder addCapability(int capability)

Add the given capability requirement to this builder.

NetworkRequest.Builder addTransportType(int transportType)

Adds the given transport requirement to this builder.

NetworkRequest build()

Build NetworkRequest give the current set of capabilities.

NetworkRequest.Builder clearCapabilities()

Completely clears all the NetworkCapabilities from this builder instance, removing even the capabilities that are set by default when the object is constructed.

NetworkRequest.Builder removeCapability(int capability)

Removes (if found) the given capability from this builder instance.

NetworkRequest.Builder removeTransportType(int transportType)

Removes (if found) the given transport from this builder instance.

NetworkRequest.Builder setNetworkSpecifier(NetworkSpecifier networkSpecifier)

Sets the optional bearer specific network specifier.

NetworkRequest.Builder setNetworkSpecifier(String networkSpecifier)

This method is deprecated. Use setNetworkSpecifier(android.net.NetworkSpecifier) instead.

Inherited methods

Public constructors

Builder

public Builder ()

Default constructor for Builder.

Public methods

addCapability

public NetworkRequest.Builder addCapability (int capability)

Add the given capability requirement to this builder. These represent the requested network's required capabilities. Note that when searching for a network to satisfy a request, all capabilities requested must be satisfied.

Parameters
capability int: The capability to add. Value is NetworkCapabilities.NET_CAPABILITY_MMS, NetworkCapabilities.NET_CAPABILITY_SUPL, NetworkCapabilities.NET_CAPABILITY_DUN, NetworkCapabilities.NET_CAPABILITY_FOTA, NetworkCapabilities.NET_CAPABILITY_IMS, NetworkCapabilities.NET_CAPABILITY_CBS, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_IA, NetworkCapabilities.NET_CAPABILITY_RCS, NetworkCapabilities.NET_CAPABILITY_XCAP, NetworkCapabilities.NET_CAPABILITY_EIMS, NetworkCapabilities.NET_CAPABILITY_NOT_METERED, NetworkCapabilities.NET_CAPABILITY_INTERNET, NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED, NetworkCapabilities.NET_CAPABILITY_TRUSTED, NetworkCapabilities.NET_CAPABILITY_NOT_VPN, NetworkCapabilities.NET_CAPABILITY_VALIDATED, NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL, NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, NetworkCapabilities.NET_CAPABILITY_FOREGROUND, NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED, NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, NetworkCapabilities.NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, or NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED

Returns
NetworkRequest.Builder The builder to facilitate chaining builder.addCapability(...).addCapability();.

addTransportType

public NetworkRequest.Builder addTransportType (int transportType)

Adds the given transport requirement to this builder. These represent the set of allowed transports for the request. Only networks using one of these transports will satisfy the request. If no particular transports are required, none should be specified here.

Parameters
transportType int: The transport type to add. Value is NetworkCapabilities.TRANSPORT_CELLULAR, NetworkCapabilities.TRANSPORT_WIFI, NetworkCapabilities.TRANSPORT_BLUETOOTH, NetworkCapabilities.TRANSPORT_ETHERNET, NetworkCapabilities.TRANSPORT_VPN, NetworkCapabilities.TRANSPORT_WIFI_AWARE, NetworkCapabilities.TRANSPORT_LOWPAN, or android.net.NetworkCapabilities.TRANSPORT_TEST

Returns
NetworkRequest.Builder The builder to facilitate chaining.

build

public NetworkRequest build ()

Build NetworkRequest give the current set of capabilities.

Returns
NetworkRequest

clearCapabilities

public NetworkRequest.Builder clearCapabilities ()

Completely clears all the NetworkCapabilities from this builder instance, removing even the capabilities that are set by default when the object is constructed.

Returns
NetworkRequest.Builder The builder to facilitate chaining. This value cannot be null.

removeCapability

public NetworkRequest.Builder removeCapability (int capability)

Removes (if found) the given capability from this builder instance.

Parameters
capability int: The capability to remove. Value is NetworkCapabilities.NET_CAPABILITY_MMS, NetworkCapabilities.NET_CAPABILITY_SUPL, NetworkCapabilities.NET_CAPABILITY_DUN, NetworkCapabilities.NET_CAPABILITY_FOTA, NetworkCapabilities.NET_CAPABILITY_IMS, NetworkCapabilities.NET_CAPABILITY_CBS, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_IA, NetworkCapabilities.NET_CAPABILITY_RCS, NetworkCapabilities.NET_CAPABILITY_XCAP, NetworkCapabilities.NET_CAPABILITY_EIMS, NetworkCapabilities.NET_CAPABILITY_NOT_METERED, NetworkCapabilities.NET_CAPABILITY_INTERNET, NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED, NetworkCapabilities.NET_CAPABILITY_TRUSTED, NetworkCapabilities.NET_CAPABILITY_NOT_VPN, NetworkCapabilities.NET_CAPABILITY_VALIDATED, NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL, NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, NetworkCapabilities.NET_CAPABILITY_FOREGROUND, NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED, NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED, android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID, NetworkCapabilities.NET_CAPABILITY_MCX, android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY, or NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED

Returns
NetworkRequest.Builder The builder to facilitate chaining.

removeTransportType

public NetworkRequest.Builder removeTransportType (int transportType)

Removes (if found) the given transport from this builder instance.

Parameters
transportType int: The transport type to remove. Value is NetworkCapabilities.TRANSPORT_CELLULAR, NetworkCapabilities.TRANSPORT_WIFI, NetworkCapabilities.TRANSPORT_BLUETOOTH, NetworkCapabilities.TRANSPORT_ETHERNET, NetworkCapabilities.TRANSPORT_VPN, NetworkCapabilities.TRANSPORT_WIFI_AWARE, NetworkCapabilities.TRANSPORT_LOWPAN, or android.net.NetworkCapabilities.TRANSPORT_TEST

Returns
NetworkRequest.Builder The builder to facilitate chaining.

setNetworkSpecifier

public NetworkRequest.Builder setNetworkSpecifier (NetworkSpecifier networkSpecifier)

Sets the optional bearer specific network specifier. This has no meaning if a single transport is also not specified, so calling this without a single transport set will generate an exception, as will subsequently adding or removing transports after this is set.

Parameters
networkSpecifier NetworkSpecifier: A concrete, parcelable framework class that extends NetworkSpecifier.

Returns
NetworkRequest.Builder

setNetworkSpecifier

public NetworkRequest.Builder setNetworkSpecifier (String networkSpecifier)

This method is deprecated.
Use setNetworkSpecifier(android.net.NetworkSpecifier) instead.

Sets the optional bearer specific network specifier. This has no meaning if a single transport is also not specified, so calling this without a single transport set will generate an exception, as will subsequently adding or removing transports after this is set.

If the networkSpecifier is provided, it shall be interpreted as follows:
  • If the specifier can be parsed as an integer, it will be treated as a TelephonyNetworkSpecifier, and the provided integer will be interpreted as a SubscriptionId.
  • If the value is an ethernet interface name, it will be treated as such.
  • For all other cases, the behavior is undefined.

Parameters
networkSpecifier String: A String of either a SubscriptionId in cellular network request or an ethernet interface name in ethernet network request.

Returns
NetworkRequest.Builder