OkHttp超时时间设置

 

OkHttp3中设置超时的方法

    public WebApi(){
        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
    }

 

捕获OkHttp3超时

然后捕获异常,加以处理。

 

    public void GetServersList(IServersListEvent serversListEvent) {
        this.serversListEvent = serversListEvent;
        serversLoadTimes = 0;
        Request request = new Request.Builder()
                .url(serversListUrl)
                .build();
        client.newCall(request).enqueue(new Callback() {
 
            @Override
            public void onFailure(Call call, IOException e) {
                if(e.getCause().equals(SocketTimeoutException.class) && serversLoadTimes

 

解析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.
 *
 * 

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 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 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 lookup(final String hostname) throws UnknownHostException {
        if (hostname == null) {
            throw new UnknownHostException("hostname == null");
        } else {
            try {
                FutureTask> task = new FutureTask<>(
                        new Callable>() {
                            @Override
                            public List 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;
    }


--------------------- 
作者:猿小二 
来源:CSDN 
原文:https://blog.csdn.net/quwei3930921/article/details/85336552 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

 

 new Thread(task).start();

 这个地方应该改用线程池,DNS解析应该是比较频繁的操作,直接开新线程不合适

你可能感兴趣的:(【Android,基础】)