使用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;
转载于:https://blog.csdn.net/quwei3930921/article/details/85336552#commentBox 感谢作者的辛苦付出
问题使用OkHttp,设备切换路由后,访问网络出现长时间无响应,很久以后才抛出UnknownHostException,这明显不是我们想要的,我们设置的connectTimeout属性似乎对dns的解析不起作用。如何解决我们先看看OkHttpClient有没有关于Dns的相关设置,发现OkHttpClient的Builder类存在dns()方法可以设置一个Dns类型参数。Dns类...
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
Android OkHttp是一个开源的HTTP客户端库,可以用来发送HTTP请求和获取数据。使用OkHttp获取数据的步骤如下:
1. 添加OkHttp依赖库到项目中。
2. 创建OkHttpClient对象,用于发送HTTP请求。
3. 创建Request对象,设置请求的URL、请求方法、请求头等信息。
4. 调用OkHttpClient的newCall方法,传入Request对象,获取Call对象。
5. 调用Call对象的execute方法,发送HTTP请求并获取Response对象。
6. 从Response对象中获取响应数据,可以是字符串、字节数组、流等形式。
7. 关闭Response对象和OkHttpClient对象,释放资源。
示例代码如下:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.example.com")
.build();
Call call = client.newCall(request);
Response response = call.execute();
String responseData = response.body().string();
response.close();
client.close();
以上代码发送一个GET请求,请求URL为http://www.example.com,获取响应数据并以字符串形式保存在responseData变量中。注意需要在子线程中执行网络请求,否则会抛出NetworkOnMainThreadException异常。