When the server is set to IPV6, Android requests the network to be extremely slow.

the reason

  • Android will give priority to ipv6 for dns processing. Why is it slow? It is still being explored... Please also advise me, thank you?

Solution

  • Reset Android line selection, prefer ipv4 (based on okhttp)
  • Code
  OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
               .readTimeout(60, TimeUnit.SECONDS)
               .writeTimeout(60, TimeUnit.SECONDS)
               .connectTimeout(30, TimeUnit.SECONDS)
               .dns(new EngDNS())
class EngDNS implements Dns {
        @Override
        public List<InetAddress> lookup(String hostname) throws UnknownHostException {
            if (TextUtils.isEmpty(hostname)) {
                return Dns.SYSTEM.lookup(hostname);
            } else {
                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 ex) {
                    return Dns.SYSTEM.lookup(hostname);
                }
            }
        }
    }

Guess you like

Origin blog.csdn.net/genmenu/article/details/98049384