Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 各電信網路類型的 WAP 代理設定(電信、移動、聯通)

編輯:關於 Android 程式設計

以下程式碼可直接嵌入專案使用,用來依據當前網路類型動態設定代理連線:

[java]
/**
 * 針對圖片下載的代理設定;
 * 取得動態代理的 HttpURLConnection
 * @param url 請求的 url 位址;
 * @return HttpURLConnection 可直接進行資料操作;
 * @throws IOException
 */
public HttpURLConnection getDynamicNetWorkHttpURLConnection(HttpURLConnection httpURLConnection, String url) throws IOException {
URL mURL = new URL(url);
if (DynamicNetWork.mConnectType == 0) {
    if (mCurrentApnName == null) {
        mCurrentApnName = getCurrentApnInUse(context);
    }
    /** 移動的代理連線 */
    if (mCurrentApnName.startsWith(CMWAP)) {
        String mDomainName = getDomainName(url);
        url = url.replace(mDomainName, mWAPLocalHost);
        httpURLConnection = (HttpURLConnection) mURL.openConnection();
        httpURLConnection.setRequestProperty("X-Online-Host", mDomainName);
    }

    /** 聯通的代理連線 */
    else if (mCurrentApnName.startsWith(UNIWAP) || mCurrentApnName.startsWith(G3WAP)) {
        InetSocketAddress inetAddress = new InetSocketAddress(mWAPLocalHost, 80);
        java.net.Proxy.Type proxyType = java.net.Proxy.Type.valueOf(mURL.getProtocol().toUpperCase());
        java.net.Proxy javaProxy = new java.net.Proxy(proxyType, inetAddress);
        httpURLConnection = (HttpURLConnection) mURL.openConnection(javaProxy);
    }

    /** 電信的代理連線 */
    else if (mCurrentApnName.startsWith(CTWAP)) {
        InetSocketAddress inetAddress = new InetSocketAddress(mCDMALocalHost, 80);
        java.net.Proxy.Type proxyType = java.net.Proxy.Type.valueOf(mURL.getProtocol().toUpperCase());
        java.net.Proxy javaProxy = new java.net.Proxy(proxyType, inetAddress);
        httpURLConnection = (HttpURLConnection) mURL.openConnection(javaProxy);
    }
} else {
    httpURLConnection = (HttpURLConnection) mURL.openConnection();
}
httpURLConnection.connect();
return httpURLConnection;
}

取得當前網路名稱的方法:

[java]
public static String getCurrentApnInUse(Context mcontext) {
    String name = "no";
    ConnectivityManager manager = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        NetworkInfo activeNetInfo = manager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.isAvailable()) {
            name = activeNetInfo.getExtraInfo();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return name;
}

取得當前代理位址的方法:

[java]
public static String getApnProxy(String currentName) {
    if ("".equals(currentName) || null == currentName) {
        return "";
    }
    currentName = currentName.toLowerCase();
    if (currentName.startsWith(CMWAP) || currentName.startsWith(UNIWAP) || currentName.startsWith(G3WAP))
        return CMCC_WAPPROXY;
    else if (currentName.startsWith(CTWAP))
        return CMCC_CDMAPROXY;
    else
        return "";
}

透過判斷 APN 名稱的前綴(如 CMWAP、UNIWAP、G3WAP、CTWAP),即可辨識當前所屬電信業者並套用對應的 WAP 代理設定,讓圖片等資源在行動網路下也能正確下載。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved