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. This topic describes a specific solution for integrating the Alibaba Cloud OSS SDK with HTTPDNS on an Android client.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a universal cloud storage service that allows developers to store and access files, such as profile pictures, images, audio, and video, in various applications. On Android, you can integrate the OSS Android SDK to use this service.
To improve the stability and speed of file transfers, you can combine the HTTPDNS SDK with the OSS Android SDK to parse OSS domain names. This method prevents domain name hijacking and accelerates DNS parsing, which optimizes the network experience when the application accesses OSS.
2. Integrate the HTTPDNS SDK based on OkHttp
You can implement the custom DNS interface of OkHttp to integrate HTTPDNS. For more information, 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
You can create an OKHttpClient instance and configure it for OSS to replace the default DNS service. For more information about how to integrate and use 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. This improves access stability and security.
The core of this implementation is to configure a custom OkHttpClient that uses HTTPDNS parsing logic for OSS.
Key points:
After you customize
OkHttpClient, you must manually transfer configurations, such as timeout and concurrency, fromClientConfigurationtoOkHttpClient.Builder. Otherwise, the original configuration becomes invalid.Make sure that the OSS SDK version is 2.9.12 or later, and initialize HTTPDNS before you initialize OSS.