All Products
Search
Document Center

:Operations for basic configurations

Last Updated:Apr 01, 2025

Obtain a service instance

You can obtain HTTPDNS service instances. HTTPDNS SDK for Android supports multiple instances, and different instances are returned for different account IDs.

Regular mode

getService

Operation definition

HttpDnsService getService(Context applicationContext, String accountID)

Category

HttpDns

Parameters

Parameter

Type

Required

Description

applicationContext

Context

Yes

The context of your Android app.

accountID

String

Yes

The ID of the account assigned by the system. After you activate HTTPDNS, you can obtain your account ID on the Development Configuration page of the EMAS consoleEMAS console.

Sample code

val httpdns = HttpDns.getService(applicationContext, accountID)
HttpDnsService httpdns = HttpDns.getService(applicationContext, accountID);

Authentication mode

getService

Operation definition

HttpDnsService getService(Context applicationContext, String accountID, String secretKey);

Category

HttpDns

Parameters

Parameter

Type

Required

Description

applicationContext

Context

Yes

The context of your Android app.

accountID

String

Yes

The ID of the account assigned by the system. After you activate HTTPDNS, you can obtain your account ID on the Development Configuration page of the EMAS consoleEMAS console.

secretKey

String

Yes

The secret key for authentication.

Sample code

val httpdns = HttpDns.getService(applicationContext, accountID, secretKey)
HttpDnsService httpdns = HttpDns.getService(applicationContext, accountID, secretKey);

Specify whether to use HTTPS requests

By default, when you use HTTPDNS SDK for Android to resolve domain names, the resolution request is initiated over HTTP. If you want to use HTTPS to initiate a resolution request, you must configure HTTPS.

setEnableHttps

Operation definition

InitConfig.Builder setEnableHttps(boolean enableHttps)

Note

This operation is added in V2.2.1.

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

enableHttps

boolean

Yes

  • Specifies whether to use HTTPS. Valid values: true and false.

Sample code

InitConfig.Builder()
    .setEnableHttps(true)
new InitConfig.Builder()
    .setEnableHttps(true);

setHTTPSRequestEnabled

Operation definition

void setHTTPSRequestEnabled(boolean enableHttps)

Warning

This operation is removed in V2.4.0. We recommend that you use the InitConfig.Builder setEnableHttps(boolean enableHttps) operation.

Category

HttpDnsService

Parameters

Parameter

Type

Required

Description

enableHttps

boolean

Yes

  • Specifies whether to use HTTPS. Valid values: true and false.

Specify whether to allow expired IP addresses to be returned

After you use the SDK to resolve a domain name, the resolution result is cached in memory. After the TTL for the resolution result has expired, the returned IP addresses are also considered expired. By default, the SDK is allowed to return expired IP addresses. If you do not want to use expired IP addresses, you must configure the SDK.

Important

If the enableExpiredIp parameter is set to true, the SDK returns the IP addresses in expired DNS records in real time and performs asynchronous updates to obtain the latest IP addresses.

setEnableExpiredIp

Operation definition

InitConfig.Builder setEnableExpiredIp(boolean enableExpiredIp)

Note

This operation is added in V2.2.1.

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

enableExpiredIp

boolean

Yes

  • Specifies whether to allow expired IP addresses to be returned. Valid values: true and false.

Sample code

InitConfig.Builder()
    .setEnableExpiredIp(true)
new InitConfig.Builder()
    .setEnableExpiredIp(true);

setExpiredIPEnabled

Operation definition

void setExpiredIPEnabled(boolean enableExpiredIp)

Warning

This operation is removed in V2.4.0. We recommend that you use the InitConfig.Builder setEnableExpiredIp(boolean enableExpiredIp) operation.

Category

HttpDnsService

Parameters

Parameter

Type

Required

Description

enableExpiredIp

boolean

Yes

  • Specifies whether to allow expired IP addresses to be returned. Valid values: true and false.

Specify whether to enable local cache

  • You can specify whether to store resolution results in the local cache. By default, local cache is disabled.

  • If you enable local cache, the SDK caches the resolution results locally for each resolution. When the application starts again, the SDK loads the cached resolution results, which improves the speed of obtaining the resolution results during application startup.

setEnableCacheIp

Operation definition

InitConfig.Builder setEnableCacheIp(boolean enableCacheIp, long expiredThresholdMillis)

Note

The operation is added in V2.4.3. If you enable local cache, you can clear the local caches that have expired for a period of time.

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

enableCacheIp

boolean

Yes

  • Specifies whether to enable local cache. Valid values: true and false.

expiredThresholdMillis

long

Yes

When you call this operation, the SDK loads resolution records from the local cache to the memory cache and clears the records whose expiration time has exceeded the value of the expiredThresholdMillis parameter.

Unit: milliseconds. The default value is 0, which indicates that expired records are cleared by default. The maximum value is 1 year.

Sample code

InitConfig.Builder()
    .setEnableCacheIp(true, DateUtils.YEAR_IN_MILLIS)
new InitConfig.Builder()
    .setEnableCacheIp(true, DateUtils.YEAR_IN_MILLIS);

setEnableCacheIp

Operation definition

InitConfig.Builder setEnableCacheIp(boolean enableCacheIp)

Note

This operation is added in V2.2.1. When you call this operation, the SDK loads resolution records from the persistent local cache to the memory cache and clears the expired DNS records.

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

enableCacheIp

boolean

Yes

  • Specifies whether to enable local cache. Valid values: true and false.

Sample code

InitConfig.Builder()
    .setEnableCacheIp(true)
new InitConfig.Builder()
    .setEnableCacheIp(true);

setCachedIPEnabled

Operation definition

void setCachedIPEnabled(boolean enableCacheIp)

void setCachedIPEnabled(boolean enableCacheIp, boolean cleanLocalCache)

Warning

This operation is removed in V2.4.0. We recommend that you use the InitConfig.Builder setEnableCacheIp(boolean enableCacheIp) operation.

Category

HttpDnsService

Parameters

Parameter

Type

Required

Description

enableCacheIp

boolean

Yes

  • Specifies whether to enable local cache. Valid values: true and false.

cleanLocalCache

boolean

No

Default value: false.

This parameter takes effect only when you set the enableCacheIp parameter to true. Valid values:

  • true: clears the DNS record in the local cache after the DNS record is read.

  • false: does not clear the DNS record in the local cache after the DNS record is read.

Specify whether to enable automatic resolution upon network changes

You can specify whether to automatically refresh the resolution results of all domain names when the network type is changed. If you enable automatic resolution, the resolution results of all domain names are automatically refreshed when the network type is changed, but traffic is generated.

Important
  • The network types include Wi-Fi, cellular, and no network.

  • If you switch between 4G and 3G, the network type is not changed.

  • If you replace your Subscriber Identity Module (SIM) card, the network type is not changed.

setPreResolveAfterNetworkChanged

Operation definition

InitConfig.Builder setPreResolveAfterNetworkChanged(boolean enable)

Note

The operation is added in V2.4.0.

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

enable

boolean

Yes

  • If you set the enable parameter to true, the domain names in your cache are refreshed and resolved when the network type is changed.

  • If you set the enable parameter to false or leave this parameter empty, the SDK deletes the cached resolution results of all domain names when the network type is changed. The domain names are resolved the next time that the domain names are accessed.

Sample code

InitConfig.Builder()
    .setPreResolveAfterNetworkChanged(true)
new InitConfig.Builder()
    .setPreResolveAfterNetworkChanged(true);

setPreResolveAfterNetworkChanged

Operation definition

void setPreResolveAfterNetworkChanged(boolean enable)

Warning

This operation is removed in V2.4.0. We recommend that you use the InitConfig.Builder setPreResolveAfterNetworkChanged(boolean enable) operation.

Category

HttpDnsService

Parameters

Parameter

Type

Required

Description

enable

boolean

Yes

  • If you set the enable parameter to true, the domain names in your cache are resolved and refreshed when the network type is changed.

  • If you set the enable parameter to false or leave this parameter empty, the SDK deletes the cached resolution results of all domain names when the network type is changed. The domain names are resolved the next time that the domain names are accessed.

Configure a timeout period

You can configure a timeout period for domain name resolution. The default timeout period is 2 seconds.

setTimeoutMillis

Note

This operation is added in V2.4.0.

Operation definition

InitConfig.Builder setTimeoutMillis(int timeoutInterval)

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

timeoutInterval

int

Yes

Unit: milliseconds. The timeout period. The default timeout period is 2 seconds. The maximum timeout period is 5 seconds.

Sample code

InitConfig.Builder()
    setTimeoutMillis.(2 * 1000)
new InitConfig.Builder()
    .setTimeoutMillis(2 * 1000);

setTimeout

Warning

This operation is deprecated in V2.4.0. We recommend that you use the InitConfig.Builder setTimeoutMillis(int timeoutInterval) operation.

Operation definition

InitConfig.Builder setTimeout(int timeoutInterval)

Category

InitConfig.Builder

Parameters

Parameter

Type

Required

Description

timeoutInterval

int

Yes

Unit: milliseconds. The timeout period. The default timeout period is 2 seconds. The maximum timeout period is 5 seconds.

Sample code

InitConfig.Builder()
    .setTimeout(2 * 1000)
new InitConfig.Builder()
    .setTimeout(2 * 1000);

setTimeoutInterval

Operation definition

void setTimeoutInterval(int timeoutInterval)

Warning

This operation is deprecated in V2.4.0. We recommend that you use the InitConfig.Builder setTimeoutMillis(int timeoutInterval) operation.

Category

HttpDnsService

Parameters

Parameter

Type

Required

Description

timeoutInterval

int

Yes

Unit: milliseconds. The timeout period. The default timeout period is 2 seconds. The maximum timeout period is 5 seconds.