首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

中國移動 CMNET 與 CMWAP 雙接入點 J2ME 開發實戰

     編輯:J2ME

中國移動 GPRS 網路提供兩種 APN 接入點:CMNET (公網直連) 與 CMWAP (WAP 閘道代理)。不同廠牌手機預設接入點不同,導致 J2ME 應用網路連線相容性問題。本文詳細分析兩種接入點特性並提供雙模相容程式碼。

CMNET:公網直連

  • 標準互聯網接入,無閘道限制
  • 可直接存取任意 IP/埠
  • Sun WTK 範例預設環境
  • Nokia S40/S60 多可自動切換

CMNET 下載實作

public byte[] downloadCMNET(String url) {
    HttpConnection conn = null;
    InputStream is = null;
    try {
        conn = (HttpConnection) Connector.open(
            "https://" + SERVER_HOST + "/" + url, Connector.READ, true);
        is = conn.openInputStream();
        
        int len = (int) conn.getLength();
        byte[] data;
        if (len == -1) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) bos.write(ch);
            data = bos.toByteArray();
            bos.close();
        } else {
            data = new byte[len];
            readFully(is, data, 0, len);
        }
        return data;
    } catch (IOException e) {
        return null;
    } finally {
        closeQuietly(is);
        closeQuietly(conn);
    }
}

CMWAP:WAP 代理閘道

  • 透過 10.0.0.172:80 代理伺服器存取外網
  • 需設定 X-Online-Host 標頭指定真實目標主機
  • Moto 手機預設僅支援 CMWAP,無法直連 CMNET
  • 部分 Nokia 可自動偵測切換

CMWAP 下載實作

public byte[] downloadCMWAP(String url) {
    HttpConnection conn = null;
    InputStream is = null;
    try {
        // 連線至 WAP 閘道,路徑帶入目標 URL
        conn = (HttpConnection) Connector.open(
            "https://10.0.0.172:80/" + url, Connector.READ, true);
        
        // 關鍵:告知閘道真實目標主機
        conn.setRequestProperty("X-Online-Host", SERVER_HOST);
        conn.setRequestProperty("Accept", "*/*");
        
        is = conn.openInputStream();
        int len = (int) conn.getLength();
        byte[] data;
        if (len == -1) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) bos.write(ch);
            data = bos.toByteArray();
            bos.close();
        } else {
            data = new byte[len];
            readFully(is, data, 0, len);
        }
        return data;
    } catch (IOException e) {
        return null;
    } finally {
        closeQuietly(is);
        closeQuietly(conn);
    }
}

雙模自適應策略

public byte[] download(String url) {
    // 1. 嘗試 CMNET (直連)
    byte[] data = downloadCMNET(url);
    if (data != null) return data;
    
    // 2. 失敗則嘗試 CMWAP (代理)
    return downloadCMWAP(url);
}

// 輔助方法
private void readFully(InputStream is, byte[] buf, int off, int len) throws IOException {
    int total = 0;
    while (total < len) {
        int read = is.read(buf, off + total, len - total);
        if (read == -1) break;
        total += read;
    }
}

private void closeQuietly(Connection c) {
    if (c != null) try { c.close(); } catch (IOException ignore) {}
}

Moto 手機使用者端設定方案

若不想在程式碼處理雙模,可引導使用者設定:

  1. 進入「網頁」→「網頁設定」
  2. 新增一個空白設定檔 (不填任何參數)
  3. 設為預設接入點
  4. J2ME 應用即可經由 CMNET 直連

關鍵技術細節

特性CMNETCMWAP
APNcmnetcmwap
Proxy無10.0.0.172:80
目標主機標頭HostX-Online-Host
支援協定TCP/UDP/HTTP/HTTPS僅 HTTP/HTTPS
埠號限制無通常限 80/443
適用裝置Nokia、Sony Ericsson 等Motorola、部分國產機

實戰建議

  • ✅ 程式碼實作雙模自動嘗試,提升相容性
  • ✅ 設定 Connector.open(..., true) 啟用逾時機制
  • ✅ getLength() 在部分 Nokia S60 回傳 -1,必須備妥分塊讀取邏輯
  • ✅ 連線建立 (openInputStream) 通常最耗時,下載過程較快
  • ✅ 真機測試覆蓋主流機型 (Moto、Nokia S40/S60、國產機)
  • ✅ 錯誤訊息友善提示:建議使用者檢查 GPRS 設定

測試驗證

開發階段可使用 WTK 模擬器配合網路監控工具 (如 Wireshark) 驗證封包路徑。上線前務必在真實移動網路環境下,以不同品牌手機進行實測。

掌握 CMNET/CMWAP 雙模開發技巧,能讓您的 J2ME 應用覆蓋絕大多數中國移動用戶,避免因接入點差異導致的連線失敗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved