Android使用手机网络访问慢,但是连接WiFi访问却很快问题解决方案!

问题:

接受公司项目后,发现自己android手机登录时请求很长(大概15s左右),但是连接WiFi后却很快(不到1s),这时间差有点大,开始怀疑网络慢,但是看视频网速正常情况下却依旧登录慢,又怀疑是网络框架的问题,项目用的是httpclient,自己用了retrofit却是依旧很慢,排除了网络框架的问题。IOS任何网络下都很正常,便确定是服务器配置方面的问题。

API启发

当我使用nslookup分析DNS域名解析发现了如下问题:
Android使用手机网络访问慢,但是连接WiFi访问却很快问题解决方案!_第1张图片
我使用cloud开头域名时解析address只有ipv4,api开头域名时有ipv6和ipv4,将app baseurl替换成只有ipv4的cloud时,使用手机网络请求就正常了,那么怀疑 Android 端解析域名时解析到两个 IP 后,优先使用 IPV6 连接的后端服务,当ipv6解析失败后,再尝试ipv4导致了时间浪费在解析时间上!

尝试使用代码解决ipv6优先ipv4解析的问题

retrofit2有个Dns接口类,源码是:

public interface Dns {
  /**
   * A DNS that uses {@link InetAddress#getAllByName} to ask the underlying operating system to
   * lookup IP addresses. Most custom {@link Dns} implementations should delegate to this instance.
   */
  Dns SYSTEM = new Dns() {
    @Override public List lookup(String hostname) throws UnknownHostException {
      if (hostname == null) throw new UnknownHostException("hostname == null");
      try {
        return Arrays.asList(InetAddress.getAllByName(hostname));
      } catch (NullPointerException e) {
        UnknownHostException unknownHostException =
            new UnknownHostException("Broken system behaviour for dns lookup of " + hostname);
        unknownHostException.initCause(e);
        throw unknownHostException;
      }
    }
  };

  /**
   * Returns the IP addresses of {@code hostname}, in the order they will be attempted by OkHttp. If
   * a connection to an address fails, OkHttp will retry the connection with the next address until
   * either a connection is made, the set of IP addresses is exhausted, or a limit is exceeded.
   */
  List lookup(String hostname) throws UnknownHostException;
}

我们通过lookup方法重写打印返回的list发现集合中ipv6在ipv4之前,那么我们通过实现此接口,将解析到的 ip 顺序调整一下,如果是 ipv4 则将其放到数据的第一个,其它保持不变,如下:

public class ApiDns implements Dns {
    @Override
    public List lookup(String hostname) throws UnknownHostException {
        if (hostname == null) {
            throw new UnknownHostException("hostname == null");
        } else {
            try {
                List mInetAddressesList = new ArrayList<>();
                InetAddress[] mInetAddresses = InetAddress.getAllByName(hostname);
                for (InetAddress address : mInetAddresses) {
                    if (address instanceof Inet4Address) {
                        mInetAddressesList.add(0, address);
                    } else {
                        mInetAddressesList.add(address);
                    }
                }
                return mInetAddressesList;
            } catch (NullPointerException var4) {
                UnknownHostException unknownHostException = new UnknownHostException("Broken system behaviour");
                unknownHostException.initCause(var4);
                throw unknownHostException;
            }
        }
    }
}

通过 okhttp 去设置dns 解析类

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.dns(new ApiDns());

再次测试,发现 Android 端的接口响应时间nice.
网上有网友说:中国移动和中国电信的 4G 网络 DNS 解析都会解析到两个 IP 地址,而中国联通的 4G 网络只能解析到 ipv4 ,让android联通的同事使用未修改之前的项目,她确实响应正常!

PS:

由于接手的项目是httpclient和retrofit都有使用,而httpclient框架未能找到类似的解决方案,所以采用了nslookup查出的cloud域名接口进行访问数据,这样改动也不大!

你可能感兴趣的:(性能优化,android网络请求)