Android Ipv6环境下接口请求缓慢解决方式<适用于OkHttp>

自定义DNS

public class OkHttpDns implements Dns {
    @Override
    public List lookup(String hostname) throws UnknownHostException {
        try{
            List 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放在最前面。

你可能感兴趣的:(Android Ipv6环境下接口请求缓慢解决方式<适用于OkHttp>)