好的,所以我终于找到了如何执行该操作。正如我在问题中所说,我正在使用OkHttpClient
设置我的 socketfactory。名称解析也是一样,使用dns
(需要OkHttp3)。
OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder()
.socketFactory(network.getSocketFactory())
.dns(NetworkDns.getInstance())
我的 NetworkDns 课程正在寻找类似的东西
public class NetworkDns implements Dns {
private static NetworkDns sInstance;
private Network mNetwork;
public static NetworkDns getInstance() {
if (sInstance == null) {
sInstance = new NetworkDns();
}
return sInstance;
}
public void setNetwork(Network network) {
mNetwork = network;
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
if (mNetwork != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return Arrays.asList(mNetwork.getAllByName(hostname));
}
return SYSTEM.lookup(hostname);
}
private NetworkDns() {
}
}
这样,当网络不为空时,它将在给定的网络上执行主机名解析。