All Products
Search
Document Center

Alibaba Cloud DNS:Best practice for using the HTTPDNS Android SDK with OkHttp on Android

Last Updated:Dec 04, 2025

This document describes how to integrate the HTTPDNS Android software development kit (SDK) into the OkHttp framework for Android.

Overview

OkHttp is a popular, lightweight, open source framework for Android that processes network requests. Developed by the mobile payment company Square as an alternative to HttpURLConnection and Apache HttpClient, OkHttp has become a preferred network framework for many Android developers. Compared with general solutions, using OkHttp with the HTTPDNS Android SDK provides two main advantages:

  • Simple implementation. You can access the HTTPDNS resolution service by implementing the DNS interface.

  • Widely applicable. No additional configurations are required for HTTPS.

Use cases

By default, OkHttp uses the system DNS service InetAddress for domain name resolution. However, OkHttp also provides a DNS interface. The design of the OkHttp framework lets you easily use the HTTPDNS Android SDK for network access by implementing this interface. No extra processing is required in complex scenarios, such as HTTPS or HTTPS with Server Name Indication (SNI). The integration is minimally intrusive.

Important

Implementing the DNS interface means that all network requests processed by the current OkHttpClient instance pass through HTTPDNS. If only some of your domain names need to be resolved by HTTPDNS, you must filter the domain names before you call the domain name resolution interface of the HTTPDNS Android SDK.

Integration guide

1. Customize the Dns API

OkHttp exposes a DNS interface. By implementing this interface, you can customize the DNS service. For the complete sample code that shows the best practices for integrating the HTTPDNS Android SDK in an OkHttp scenario, see the Demo project source code.

  1. Create a new class in the project directory to implement the Dns API.

  2. Override the lookup method in the custom DNS according to the API provided in the Android SDK development guide to perform domain name resolution operations.

  public class OkHttpDns implements Dns {

    private static OkHttpDns instance;
    private static Object lock = new Object();
    private DNSResolver mDNSResolver = DNSResolver.getInstance();

    private OkHttpDns() {
    }

    public static OkHttpDns getInstance() {
        if (null == instance) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new OkHttpDns();
                }
            }
        }
        return instance;
    }

    @Override
    public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
        // Call the API provided by the HTTPDNS Android SDK to resolve the domain name.
        String[] IPArray = mDNSResolver.getIpv4ByHostFromCache(hostname,true);
        if (IPArray == null || IPArray.length == 0){
            IPArray = mDNSResolver.getIPsV4ByHost(hostname);
        }
        if (IPArray != null && IPArray.length > 0) {
            List<InetAddress> inetAddresses = new ArrayList<>();
            InetAddress address;
            for (String ip : IPArray) {
                Log.d(TAG, "mDnsCache IP: " +hostname+ ip);
                address = InetAddress.getByName(ip);
                inetAddresses.add(address);
            }
            if (!inetAddresses.isEmpty()) {
                return inetAddresses;
            }
        }
        // If null is returned, the system DNS service resolves the domain name.
        return okhttp3.Dns.SYSTEM.lookup(hostname);
    }
}

2. Create an OkHttpClient

public void okhttpDnsRequest(String url, final Handler mHandler) {
        OkHttpClient client = new OkHttpClient.Builder()
                .dns(OkHttpDns.getInstance())
                .build();

        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = null;
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                }

                DataInputStream dis = new DataInputStream(response.body().byteStream());
                int len;
                byte[] buff = new byte[4096];
                StringBuilder result = new StringBuilder();
                while ((len = dis.read(buff)) != -1) {
                    result.append(new String(buff, 0, len));
                }

                String resultInfo = result.toString();
                Log.d("OkHttpDns", "Response: " + resultInfo);
             }
        });
    }
Important
  1. These best practices apply only to scenarios where you integrate the HTTPDNS Android SDK with OkHttp.

  2. For more information about how to use the domain name resolution service of the HTTPDNS Android SDK and how to integrate the HTTPDNS Android SDK, see the Android SDK Development Guide.

  3. For the complete sample code that shows the best practices for integrating the HTTPDNS Android SDK in an OkHttp framework scenario, see the Demo project source code.