DnsResolver
public
final
class
DnsResolver
extends Object
| java.lang.Object | |
| ↳ | android.net.DnsResolver |
Dns resolver class for asynchronous dns querying Note that if a client sends a query with more than 1 record in the question section but the remote dns server does not support this, it may not respond at all, leading to a timeout.
Summary
Nested classes | |
|---|---|
interface |
DnsResolver.Callback<T>
Base interface for answer callbacks |
class |
DnsResolver.DnsException
Class to represent DNS error |
Constants | |
|---|---|
int |
CLASS_IN
|
int |
ERROR_PARSE
Indicates that there was an error parsing the response the query. |
int |
ERROR_SYSTEM
Indicates that there was an error sending the query. |
int |
FLAG_EMPTY
|
int |
FLAG_NO_CACHE_LOOKUP
|
int |
FLAG_NO_CACHE_STORE
|
int |
FLAG_NO_RETRY
|
int |
HTTPS_QUERY_WAIT_AUTO
Indicates that the default wait time should be used for the HTTPS query. |
int |
HTTPS_QUERY_WAIT_NONE
Indicates that no additional wait should be used for the HTTPS query. |
int |
HTTPS_QUERY_WAIT_UNTIL_TIMEOUT
Indicates that the HTTPS query will be retransmitted until it gets a response, or until it times out. |
int |
TYPE_A
|
int |
TYPE_AAAA
|
int |
TYPE_HTTPS
|
Public methods | |
|---|---|
static
DnsResolver
|
getInstance(Context context, Looper looper)
Returns a |
static
DnsResolver
|
getInstance()
This method is deprecated.
Use |
void
|
query(Network network, String domain, int flags, Executor executor, int httpsTimeoutMillis, CancellationSignal cancellationSignal, Callback<HttpsEndpoint> callback)
Send concurrent A/AAAA/HTTPS DNS queries with the specified name on a network. |
void
|
query(Network network, String domain, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super List<InetAddress>> callback)
Send a DNS query with the specified name on a network with both IPv4 and IPv6, get back a set of InetAddresses with rfc6724 sorting asynchronously. |
void
|
query(Network network, String domain, int nsType, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super List<InetAddress>> callback)
Send a DNS query with the specified name and query type, get back a set of InetAddresses with rfc6724 sorting asynchronously. |
void
|
rawQuery(Network network, byte[] query, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super byte[]> callback)
Send a raw DNS query. |
void
|
rawQuery(Network network, String domain, int nsClass, int nsType, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super byte[]> callback)
Send a DNS query with the specified name, class and query type. |
Inherited methods | |
|---|---|
Constants
ERROR_PARSE
public static final int ERROR_PARSE
Indicates that there was an error parsing the response the query.
The cause of this error is available via getCause() and is a ParseException.
Constant Value: 0 (0x00000000)
ERROR_SYSTEM
public static final int ERROR_SYSTEM
Indicates that there was an error sending the query. The cause of this error is available via getCause() and is an ErrnoException.
Constant Value: 1 (0x00000001)
FLAG_NO_CACHE_LOOKUP
public static final int FLAG_NO_CACHE_LOOKUP
Constant Value: 4 (0x00000004)
FLAG_NO_CACHE_STORE
public static final int FLAG_NO_CACHE_STORE
Constant Value: 2 (0x00000002)
FLAG_NO_RETRY
public static final int FLAG_NO_RETRY
Constant Value: 1 (0x00000001)
HTTPS_QUERY_WAIT_AUTO
public static final int HTTPS_QUERY_WAIT_AUTO
Indicates that the default wait time should be used for the HTTPS query.
This option balances the extra latency of waiting for the HTTPS record with the latency and security benefits of receiving it.
Constant Value: -1 (0xffffffff)
HTTPS_QUERY_WAIT_NONE
public static final int HTTPS_QUERY_WAIT_NONE
Indicates that no additional wait should be used for the HTTPS query.
This option means the callback will be called immediately as soon as the IP address queries have received a response, regardless of whether the HTTPS query has received a response or not.
This option is not recommended for security or latency because it may result in HTTPS records not being returned even if they exist.
Constant Value: 0 (0x00000000)
HTTPS_QUERY_WAIT_UNTIL_TIMEOUT
public static final int HTTPS_QUERY_WAIT_UNTIL_TIMEOUT
Indicates that the HTTPS query will be retransmitted until it gets a response, or until it times out.
This option may increase latency if the HTTPS query is dropped by the network.
Constant Value: -2 (0xfffffffe)
Public methods
getInstance
public static DnsResolver getInstance (Context context, Looper looper)
Returns a DnsResolver instance.
| Parameters | |
|---|---|
context |
Context: used for internal interactions with other system services.
This value cannot be null. |
looper |
Looper: Looper for monitoring incoming replies to DNS queries. If null, then
uses the value returned by Looper.getMainLooper(). |
| Returns | |
|---|---|
DnsResolver |
This value cannot be null. |
getInstance
public static DnsResolver getInstance ()
This method is deprecated.
Use getInstance(Context,Looper) instead.
Get instance for DnsResolver
| Returns | |
|---|---|
DnsResolver |
This value cannot be null. |
query
public void query (Network network, String domain, int flags, Executor executor, int httpsTimeoutMillis, CancellationSignal cancellationSignal, Callback<HttpsEndpoint> callback)
Send concurrent A/AAAA/HTTPS DNS queries with the specified name on a network.
The answer to all queries will be provided asynchronously through the provided
callback Callback, which provides a HttpsEndpoint.
| Parameters | |
|---|---|
network |
Network: Network specifying which network to query on.
null for query on default network. |
domain |
String: domain name to query.
This value cannot be null. |
flags |
int: flags as a combination of the FLAGS_* constants.
Value is one of the following: |
executor |
Executor: The Executor that the callback should be executed on.
This value cannot be null.
Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
httpsTimeoutMillis |
int: the timeout in milliseconds to wait for the HTTPS query to complete
after the A/AAAA queries have already completed. May be set to
HTTPS_QUERY_WAIT_NONE to disable any additional wait,
HTTPS_QUERY_WAIT_AUTO to use the default wait time,
HTTPS_QUERY_WAIT_UNTIL_TIMEOUT to wait for the HTTPS query to complete, or a
specific value in milliseconds. |
cancellationSignal |
CancellationSignal: used by the caller to signal if all the queries should be
cancelled. May be null. |
callback |
Callback: a Callback which will be called to notify the caller of the results
of the A/AAAA/HTTPS DNS queries. Will return a ERROR_PARSE if any of the DNS
records cannot be parsed, and should be treated as a resolution failure.
This value cannot be null. |
query
public void query (Network network, String domain, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super List<InetAddress>> callback)
Send a DNS query with the specified name on a network with both IPv4 and IPv6,
get back a set of InetAddresses with rfc6724 sorting asynchronously.
This method will examine the connection ability on given network, and query IPv4
and IPv6 if connection is available. This method will send A queries if the specified
Network provides IPv4 connectivity, and AAAA queries if it provides IPv6
connectivity.
If at least one query succeeded with valid answer, rcode will be 0
The answer will be provided asynchronously through the provided Callback.
| Parameters | |
|---|---|
network |
Network: Network specifying which network to query on.
null for query on default network. |
domain |
String: domain name to query.
This value cannot be null. |
flags |
int: flags as a combination of the FLAGS_* constants.
Value is one of the following: |
executor |
Executor: The Executor that the callback should be executed on.
This value cannot be null.
Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
cancellationSignal |
CancellationSignal: used by the caller to signal if the query should be
cancelled. May be null. |
callback |
Callback: a Callback which will be called to notify the
caller of the result of dns query.
This value cannot be null. |
query
public void query (Network network, String domain, int nsType, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super List<InetAddress>> callback)
Send a DNS query with the specified name and query type, get back a set of
InetAddresses with rfc6724 sorting asynchronously.
The answer will be provided asynchronously through the provided Callback.
| Parameters | |
|---|---|
network |
Network: Network specifying which network to query on.
null for query on default network. |
domain |
String: domain name to query.
This value cannot be null. |
nsType |
int: dns resource record (RR) type as one of the TYPE_* constants.
Value is one of the following: |
flags |
int: flags as a combination of the FLAGS_* constants.
Value is one of the following: |
executor |
Executor: The Executor that the callback should be executed on.
This value cannot be null.
Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
cancellationSignal |
CancellationSignal: used by the caller to signal if the query should be
cancelled. May be null. |
callback |
Callback: a Callback which will be called to notify the caller
of the result of dns query.
This value cannot be null. |
rawQuery
public void rawQuery (Network network, byte[] query, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super byte[]> callback)
Send a raw DNS query.
The answer will be provided asynchronously through the provided Callback.
| Parameters | |
|---|---|
network |
Network: Network specifying which network to query on.
null for query on default network. |
query |
byte: blob message to query.
This value cannot be null. |
flags |
int: flags as a combination of the FLAGS_* constants.
Value is one of the following: |
executor |
Executor: The Executor that the callback should be executed on.
This value cannot be null.
Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
cancellationSignal |
CancellationSignal: used by the caller to signal if the query should be
cancelled. May be null. |
callback |
Callback: a Callback which will be called to notify the caller
of the result of dns query.
This value cannot be null. |
rawQuery
public void rawQuery (Network network, String domain, int nsClass, int nsType, int flags, Executor executor, CancellationSignal cancellationSignal, Callback<? super byte[]> callback)
Send a DNS query with the specified name, class and query type.
The answer will be provided asynchronously through the provided Callback.
| Parameters | |
|---|---|
network |
Network: Network specifying which network to query on.
null for query on default network. |
domain |
String: domain name to query.
This value cannot be null. |
nsClass |
int: dns class as one of the CLASS_* constants.
Value is one of the following: |
nsType |
int: dns resource record (RR) type as one of the TYPE_* constants.
Value is one of the following: |
flags |
int: flags as a combination of the FLAGS_* constants.
Value is one of the following: |
executor |
Executor: The Executor that the callback should be executed on.
This value cannot be null.
Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
cancellationSignal |
CancellationSignal: used by the caller to signal if the query should be
cancelled. May be null. |
callback |
Callback: a Callback which will be called to notify the caller
of the result of dns query.
This value cannot be null. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-02-26 UTC.