The Android SDK integration process topic provides a complete guide to importing and configuring the Android software development kit (SDK), resolving IP addresses, applying them to your network library, and verifying the integration. This topic focuses on the specific solution for integrating HTTPDNS with OkHttp.
1. Background
If you use OkHttp as the network framework on Android, you can elegantly integrate HTTPDNS services by calling the custom DNS service interface provided by OkHttp.
OkHttp is an open source lightweight Android framework for processing network requests. It is developed by Square as an alternative to HttpURLConnection and Apache HttpClient. Since its release, OkHttp has gained popularity among Android developers as it has matured.
By default, OkHttp uses the system DNS service InetAddress for domain name resolution. It also exposes an interface to customize the DNS service, which you can use to integrate HTTPDNS.
This method also applies to Retrofit+OkHttp. You can pass the configured OkHttpClient as a parameter to Retrofit.Builder::client(OkHttpClient).
2. Custom DNS interface
OkHttp provides the Dns API for developers to customize DNS resolution:
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;
}
@Override
public 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);
}
}The internal retry mechanism in okhttp may select different IP addresses to establish a connection during retries. Therefore, the lookup() method in the Dns interface can return a List<InetAddress> that contains multiple IP addresses. This approach prevents request failures that can occur if a single, unavailable IP address is returned.
3. Configure OkHttpClient
Create an OkHttpClient object and pass in the OkHttpDns object instead of the default DNS service:
private fun initOkHttp(context: Context) {
val client = OkHttpClient.Builder()
// Configure other initialization settings of OkHttp as required
.dns(OkHttpDns(context))
.build()
}private void initOkHttp(Context context) {
OkHttpClient client = new OkHttpClient.Builder()
// Configure other initialization settings of OkHttp as required
.dns(new OkHttpDns(context))
.build();
}The following is the complete code for the OkHttp and HttpDns implementation.
4. Verification
After the integration is complete, you must verify that it was successful. You can use methods such as hijacking simulation or fault injection testing. For more information, see Verifying the network library integration.
5. Summary
The combination of OkHttp and HTTPDNS provides the following benefits:
You only need to implement an API for DNS service customization to access HTTPDNS.
This solution applies to scenarios where HTTPS, Server Name Indication (SNI), and cookies are configured. This solution eliminates the need to perform certificate verification and domain name check.