All Products
Search
Document Center

:Initialize configurations

最終更新日:Sep 22, 2022

Obtain a service instance

This operation obtains an HTTPDNS service instance. Different instances are returned for different account IDs.

Operation definition

HttpDnsService getService(Context applicationContext, String accountID);

Parameters

Parameter

Type

Required

Description

applicationContext

Context

Yes

The Android app context.

accountID

String

Yes

The account ID, which can be obtained from the homepage of the HTTPDNS console.

Sample code

HttpDnsService httpdns = HttpDns.getService(getApplicationContext(), accountId);

Obtain an HTTPDNS service instance with authentication

Operation definition

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

Parameters

Parameter

Type

Required

Description

applicationContext

Context

Yes

The Android app context.

accountID

String

Yes

The account ID assigned by the system. After you activate HTTPDNS, you can query your account ID on the Overview page of the HTTPDNS console.

You can also query the account ID from the downloaded configuration file.

secretKey

String

Yes

The secret key used to authenticate access to the API operation.

Sample code

        HttpDnsService httpdns = HttpDns.getService(getApplicationContext(), accountId, secretKey);

Set domain names for pre-resolution

This operation allows you to set domain names of apps for pre-resolution by HTTPDNS.

Operation definition

void setPreResolveHosts(ArrayList<String> hostList);
void setPreResolveHosts(ArrayList<String> hostList, RequestIpType requestIpType);

Parameters

Parameter

Type

Required

Description

hostList

ArrayList<String>

Yes

An array that consists of the domain names that you want to pre-resolve.

requestIpType

RequestIpType

No

The IP address type that corresponds to the domain name that you want to pre-resolve. By default, a domain name is pre-resolved to an IPv4 address.

Sample code

        // httpdns specifies the service instance that is obtained after initialization. By default, the domain names are pre-resolved to IPv4 addresses.
        httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")));
        // httpdns specifies the service instance that is obtained after initialization. The domain names are pre-resolved to IPv4 addresses.
        httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.v4);
        // httpdns specifies the service instance that is obtained after initialization. The domain names are pre-resolved to IPv6 addresses.
        httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.v6);
        // httpdns specifies the service instance that is obtained after initialization. The domain names are pre-resolved to both IPv4 and IPv6 addresses.
        httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.both);

Initialize configurations

This operation configures some properties of a service instance before the service instance is obtained. You can choose to not initialize configurations for a service instance. You can also choose to specify all or specific configuration items after the service instance is obtained. Note: You must initialize configurations before the service instance is obtained. Otherwise, the configurations do not take effect.

Operation definition

public class InitConfig {
    public static class Builder {

        /**
         * Specify whether to allow expired IP addresses to be returned.
         * @param enableExpiredIp
         * @return
         */
        public Builder setEnableExpiredIp(boolean enableExpiredIp)

        /**
         * Specify whether to allow local persistent caching of resolution results.
         * @param enableCacheIp
         * @return
         */
        public Builder setEnableCacheIp(boolean enableCacheIp)

        /**
         * Specify the service request timeout period.
         * @param timeout
         * @return
         */
        public Builder setTimeout(int timeout)

        /**
         * Specify whether to enable HTTPS.
         * @param enableHttps
         * @return
         */
        public Builder setEnableHttps(boolean enableHttps)

        /**
         // Specify a list of IPv4 addresses that you want to probe to obtain the optimal IPv4 address.
         * @param ipProbeItems
         * @return
         */
        public Builder setIpProbeItems(List<IPProbeItem> ipProbeItems)

        /**
         * Specify a region.
         * @param region
         * @return
         */
        public Builder setRegion(String region)

        /**
         * Create configurations and record the ID of the account to which the configurations belong.
         * @param accountId
         * @return
         */
        public InitConfig buildFor(String accountId)
    }
}

Configuration methods

Method

Type

Required

Description

setEnableExpiredIp

boolean

No

By default, the system allows expired IP addresses to be returned. This method serves the same purpose as setExpiredIPEnabled.

setEnableCacheIp

boolean

No

By default, persistent caching of resolution results is not enabled. This method serves the same purpose as setCachedIPEnabled.

setTimeout

int

No

By default, the timeout period is set to 15,000 milliseconds. This method serves the same purpose as setTimeoutInterval.

setEnableHttps

boolean

No

By default, HTTPS is disabled. This method serves the same purpose as setHTTPSRequestEnabled.

setIpProbeItems

List<IPProbeItem>

No

By default, the IP address list is not probed. This method serves the same purpose as setIPProbeList.

setRegion

String

No

By default, domain names are resolved by servers in the Chinese mainland. This method serves the same purpose as setRegion.

buildFor

String

Yes

This method creates configurations. Created configurations take effect only if you call this method. The accountId parameter indicates the account ID, which can be obtained from the HTTPDNS console.

Sample code

        // Initialize configurations.
        new InitConfig.Builder()
                // Specify the initial region. By default, a region in the Chinese mainland is used.
                .setRegion(currentRegion)
                // Specify whether to enable HTTPS. By default, HTTP is enabled.
                .setEnableHttps(enableHttps)
                // Specify the timeout period of a service request. Unit: milliseconds.
                .setTimeout(10 * 1000)
                // Specify whether to enable local caching. By default, local caching is disabled.
                .setEnableCacheIp(true)
                // Specify whether to allow expired IP addresses to be returned. By default, expired IP addresses can be returned.
                .setEnableExpiredIp(true)
                // Specify a list of IPv4 addresses that you want to probe to obtain the optimal IPv4 address.
                .setIpProbeItems(list)
                // Specify the account ID.
                .buildFor(accountID);