Android使用手機網絡訪問慢,但是連接WiFi訪問卻很快問題解決方案!

問題:

接受公司項目後,發現自己android手機登錄時請求很長(大概15s左右),但是連接WiFi後卻很快(不到1s),這時間差有點大,開始懷疑網絡慢,但是看視頻網速正常情況下卻依舊登錄慢,又懷疑是網絡框架的問題,項目用的是httpclient,自己用了retrofit卻是依舊很慢,排除了網絡框架的問題。IOS任何網絡下都很正常,便確定是服務器配置方面的問題。

API啓發

當我使用nslookup分析DNS域名解析發現瞭如下問題:
在這裏插入圖片描述
我使用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<InetAddress> 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<InetAddress> lookup(String hostname) throws UnknownHostException;
}

我們通過lookup方法重寫打印返回的list發現集合中ipv6在ipv4之前,那麼我們通過實現此接口,將解析到的 ip 順序調整一下,如果是 ipv4 則將其放到數據的第一個,其它保持不變,如下:

public class ApiDns implements Dns {
    @Override
    public List<InetAddress> lookup(String hostname) throws UnknownHostException {
        if (hostname == null) {
            throw new UnknownHostException("hostname == null");
        } else {
            try {
                List<InetAddress> 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域名接口進行訪問數據,這樣改動也不大!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章