Android笔记之解决OkHttp解析dns超时时间无法设置的问题

问题

使用OkHttp,设备切换路由后,访问网络出现长时间无响应,很久后来才抛出UnknownHostException,这明显不是我们想要的,我们设置的connectTimeout属性似乎对dns的解析不起作用。

如何解决

我们先看看OkHttpClient有没有关于Dns的相关设置,发现OkHttpClient的Builder类存在dns()方法可以设置一个Dns类型参数。

Dns类源码如下:

package okhttp3;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Arrays;

import java.util.List;

/**

* A domain name service that resolves IP addresses for host names. Most applications will use the

* {@linkplain #SYSTEM system DNS service}, which is the default. Some applications may provide

* their own implementation to use a different DNS server, to prefer IPv6 addresses, to prefer IPv4

* addresses, or to force a specific known IP address.

*

* <p>Implementations of this interface must be safe for concurrent use.

*/

public interface Dns {

  /**

  * A DNS that uses {@link InetAddress#getAllByName} to ask the underlying operating system to

  * lookup IP addresses. Most custom {@link Dns} implementations should delegate to this instance.

  */

  Dns SYSTEM = new Dns() {

    @Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {

      if (hostname == null) throw new UnknownHostException(“hostname == null”);

      try {

        return Arrays.asList(InetAddress.getAllByName(hostname));

      } catch (NullPointerException e) {

        UnknownHostException unknownHostException =

            new UnknownHostException(“Broken system behaviour for dns lookup of ” + hostname);

        unknownHostException.initCause(e);

        throw unknownHostException;

      }

    }

  };

  /**

  * Returns the IP addresses of {@code hostname}, in the order they will be attempted by OkHttp. If

  * a connection to an address fails, OkHttp will retry the connection with the next address until

  * either a connection is made, the set of IP addresses is exhausted, or a limit is exceeded.

  */

  List<InetAddress> lookup(String hostname) throws UnknownHostException;

}

这是一个接口类,并且其中已经写好了默认实现并赋值给了SYSTEM对象。

透过代码我们可以看出,解析dns主要是靠这行代码:

InetAddress.getAllByName(hostname)

Ok,我们已经知道如何解析dns并设置给OkHttpClient了,目前我们只需要重新实现这个过程,插入超时控制,并让OKHttp调用新的Dns解析就好了。

实现

实现抽象类Dns

package com.x.http;

import okhttp3.Dns;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.Arrays;

import java.util.List;

import java.util.concurrent.Callable;

import java.util.concurrent.FutureTask;

import java.util.concurrent.TimeUnit;

public class XDns implements Dns {

    private long timeout;

    public XDns(long timeout) {

        this.timeout = timeout;

    }

    @Override

    public List<InetAddress> lookup(final String hostname) throws UnknownHostException {

        if (hostname == null) {

            throw new UnknownHostException(“hostname == null”);

        } else {

            try {

                FutureTask<List<InetAddress>> task = new FutureTask<>(

                        new Callable<List<InetAddress>>() {

                            @Override

                            public List<InetAddress> call() throws Exception {

                                return Arrays.asList(InetAddress.getAllByName(hostname));

                            }

                        });

                new Thread(task).start();

                return task.get(timeout, TimeUnit.MILLISECONDS);

            } catch (Exception var4) {

                UnknownHostException unknownHostException =

                        new UnknownHostException(“Broken system behaviour for dns lookup of ” + hostname);

                unknownHostException.initCause(var4);

                throw unknownHostException;

            }

        }

    }

}

上面的实现主要依赖了FutureTask可以设置任务执行时间的特性,不清楚的同学可以自行学习一下。

然后把新的dns解析类设置给OkHttpClient

    public static OkHttpClient createClient(long timeout, long writeTimeout,

    long readTimeout, boolean bRetry) {

        OkHttpClient.Builder builder = new OkHttpClient.Builder()

                .connectTimeout(timeout, TimeUnit.MILLISECONDS)

                .dns(new XDns(timeout))

                .retryOnConnectionFailure(bRetry);

        if (writeTimeout > 0)

            builder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);

        if (readTimeout > 0)

            builder.readTimeout(readTimeout, TimeUnit.MILLISECONDS);

        OkHttpClient client = builder.build();

        client.dispatcher().setMaxRequestsPerHost(10);

        client.dispatcher().setMaxRequests(30);

        return client;

    }

原文链接

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容