The Android SDK integration process topic explains how to import and configure the Android SDK, resolve IP addresses, apply the SDK to a network library, and verify the integration. You can then integrate the Alibaba Cloud OSS SDK with HTTPDNS on your Android client by following this guide.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a cloud storage service for storing and accessing files such as images, audio, and video. On Android, you integrate OSS through the OSS Android SDK.
Combining the HTTPDNS SDK with the OSS Android SDK lets you resolve OSS domain names through HTTPDNS. This prevents domain name hijacking and speeds up DNS resolution, improving network performance when your application accesses OSS.
2. Integrate the HTTPDNS SDK based on OkHttp
Implement the custom DNS interface of OkHttp to integrate HTTPDNS. For details, see Best practices for using HTTPDNS with OkHttp on Android.
class OkHttpDns constructor(context: Context): Dns {
private var mContext: Context
init {
mContext = context
}
@Throws(UnknownHostException::class)
override fun lookup(host: String): List<InetAddress> {
val httpdnsResult: HTTPDNSResult = HttpDns.getService(accountID)
.getHttpDnsResultForHostSync(host, RequestIpType.both)
val inetAddresses: MutableList<InetAddress> = ArrayList()
var address: InetAddress
try {
if (httpdnsResult.ips != null) {
// Process IPv4 addresses
for (ipv4 in httpdnsResult.ips) {
address = InetAddress.getByName(ipv4)
inetAddresses.add(address)
}
}
if (httpdnsResult.ipv6s != null) {
// Process IPv6 addresses
for (ipv6 in httpdnsResult.ipv6s) {
address = InetAddress.getByName(ipv6)
inetAddresses.add(address)
}
}
} catch (e: UnknownHostException) {
}
return if (!inetAddresses.isEmpty()) {
inetAddresses
} else Dns.SYSTEM.lookup(host)
}
}public class OkHttpDns implements Dns {
private Context mContext;
public OkHttpDns(Context context) {
mContext = context;
}
@Overridepublic List<InetAddress> lookup(String host) throws UnknownHostException {
HTTPDNSResult httpdnsResult = HttpDns.getService(accountID).getHttpDnsResultForHostSync(host, RequestIpType.both);
List<InetAddress> inetAddresses = new ArrayList<>();
InetAddress address;
try {
if (httpdnsResult.getIps() != null) {
// Process IPv4 addresses
for (String ipv4 : httpdnsResult.getIps()) {
address = InetAddress.getByName(ipv4);
inetAddresses.add(address);
}
}
if (httpdnsResult.getIpv6s() != null) {
// Process IPv6 addresses
for (String ipv6 : httpdnsResult.getIpv6s()) {
address = InetAddress.getByName(ipv6);
inetAddresses.add(address);
}
}
} catch (UnknownHostException e) {
}
if (!inetAddresses.isEmpty()) {
return inetAddresses;
}
return okhttp3.Dns.SYSTEM.lookup(host);
}
}3. Integrate the OSS SDK and use HTTPDNS
Create an OkHttpClient instance configured with HTTPDNS to replace the default DNS service for OSS. For details about integrating the OSS SDK, see OSS Android SDK.
// Create an OSS client configuration.
val conf = ClientConfiguration()
conf.connectionTimeout = 15 * 1000 // Connection timeout. Default value: 15 seconds.
conf.socketTimeout = 15 * 1000 // Socket timeout. Default value: 15 seconds.
conf.maxConcurrentRequest = 5 // Maximum number of concurrent requests. Default value: 5.
conf.maxErrorRetry = 2 // Maximum number of retries upon failure. Default value: 2.
val builder = OkHttpClient.Builder()
.dns(OkHttpDns(applicationContext))
// If you set a custom okHttpClient, some settings of ClientConfiguration will become invalid. You need to manually set them on the builder.
if (conf != null) {
val dispatcher = Dispatcher()
dispatcher.maxRequests = conf.maxConcurrentRequest
builder.connectTimeout(conf.connectionTimeout.toLong(), TimeUnit.MILLISECONDS)
.readTimeout(conf.socketTimeout.toLong(), TimeUnit.MILLISECONDS)
.writeTimeout(conf.socketTimeout.toLong(), TimeUnit.MILLISECONDS)
.followRedirects(conf.isFollowRedirectsEnable)
.followSslRedirects(conf.isFollowRedirectsEnable)
.dispatcher(dispatcher)
if (conf.proxyHost != null && conf.proxyPort != 0) {
builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(conf.proxyHost, conf.proxyPort)))
}
}
// Only Android OSS SDK 2.9.12 or later supports the conf.setOkHttpClient() method.
conf.okHttpClient = builder.build()
val oss = OSSClient(applicationContext, endpoint, credentialProvider, conf)
// Create an OSS client configuration.
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // Connection timeout. Default value: 15 seconds.
conf.setSocketTimeout(15 * 1000); // Socket timeout. Default value: 15 seconds.
conf.setMaxConcurrentRequest(5); // Maximum number of concurrent requests. Default value: 5.
conf.setMaxErrorRetry(2); // Maximum number of retries upon failure. Default value: 2.
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dns(new OkHttpDns(getApplicationContext()));
// If you set a custom okHttpClient, some settings of ClientConfiguration will become invalid. You need to manually set them on the builder.
if (conf != null) {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(conf.getMaxConcurrentRequest());
builder.connectTimeout(conf.getConnectionTimeout(), TimeUnit.MILLISECONDS)
.readTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
.followRedirects(conf.isFollowRedirectsEnable())
.followSslRedirects(conf.isFollowRedirectsEnable())
.dispatcher(dispatcher);
if (conf.getProxyHost() != null && conf.getProxyPort() != 0) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(conf.getProxyHost(), conf.getProxyPort())));
}
}
// Only Android OSS SDK 2.9.12 or later supports the conf.setOkHttpClient() method.
conf.setOkHttpClient(builder.build());
OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);The HTTPDNS SDK must be initialized before the OSS SDK is initialized.
4. Summary
Combining the HTTPDNS SDK with the OSS SDK prevents DNS hijacking and accelerates file transfers, improving access stability and security.
The core step is configuring a custom OkHttpClient that uses HTTPDNS resolution for OSS.
Key points:
-
When you use a custom
OkHttpClient, manually transfer configurations such as timeout and concurrency fromClientConfigurationtoOkHttpClient.Builder. Otherwise, the original settings are not applied. -
Ensure the OSS SDK version is 2.9.12 or later, and initialize HTTPDNS before OSS.