public class DnsFactory {
public static Dns getDns() {
switch (Environment.getType()) {
case Environment.TYPE_TEST:
return new DevDns();
case Environment.TYPE_PRE_RELEASE:
return new PreReleaseDns();
case Environment.TYPE_ONLINE:
default:
return Dns.SYSTEM;
}
}
// DevDns
private static class DevDns implements Dns {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
return SYSTEM.lookup(hostname);
}
}
// PreReleaseDns
private static class PreReleaseDns implements Dns {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
// hostname ip white list,set Host to IP manually
if ("xxxx".equals(hostname)
|| "xxxx".equals(hostname)
|| "xxxx".equals(hostname)
|| "xxxx".equals(hostname)) {
InetAddress byAddress = InetAddress.getByAddress(hostname, new byte[]{xx,xx,xx,xx});
return Collections.singletonList(byAddress);
} else {
return SYSTEM.lookup(hostname);
}
}
}
}
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
builder.dns(DnsFactory.getDns());