自定义DNS
public class OkHttpDns implements Dns {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
try{
List<InetAddress> inetAddressList = new ArrayList<>();
InetAddress[] inetAddresses = InetAddress.getAllByName (hostname) ;
for (InetAddress inetAddress: inetAddresses){
if (inetAddress instanceof Inet4Address){
inetAddressList.add(0, inetAddress) ;
}else{
inetAddressList.add(inetAddress);
}
}
return inetAddressList;
}catch (NullPointerException var4){
UnknownHostException unknownHostException = new UnknownHostException("Broken system behavior");
unknownHostException.initCause(var4);
throw unknownHostException;
}
}
}
设置自定义的DNS
okHttpBuilder.dns(new OkHttpDns());
原因:Android 默认不支持ipv64且OkHttp默认ipv64放在最前面。