| 協議 | scheme | target 格式示例 | 返回接口類型 |
| HTTP | http | //host:port/path?query | HttpConnection |
| HTTPS | https | //host:port/path | HttpsConnection |
| TCP Client | socket | //host:port | SocketConnection |
| TCP Server | socket | //:port (host 為空) | ServerSocketConnection |
| UDP | datagram | //host:port | UDPDatagramConnection |
| 文件 (JSR 75) | file | ///path/to/file | FileConnection |
實戰代碼模板
1. HTTP GET 請求標準模式
HttpConnection http = null;
InputStream is = null;
try {
String url = "https://api.example.com/data?param=value";
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
http.setRequestProperty("User-Agent", "MyMidlet/1.0");
http.setRequestProperty("Accept", "application/json");
int rc = http.getResponseCode();
if (rc == HttpConnection.HTTP_OK) {
is = http.openInputStream();
// 讀取數據...
} else {
// 錯誤處理
}
} catch (IOException e) {
// 網絡異常
} finally {
if (is != null) try { is.close(); } catch (IOException ignored) {}
if (http != null) try { http.close(); } catch (IOException ignored) {}
}
2. TCP Socket 客戶端
SocketConnection socket = null;
InputStream is = null; OutputStream os = null;
try {
socket = (SocketConnection) Connector.open("socket://192.168.1.100:8888");
socket.setSocketOption(SocketConnection.DELAY, 0); // 關閉 Nagle
socket.setSocketOption(SocketConnection.LINGER, 5); // 延遲關閉 5s
is = socket.openInputStream();
os = socket.openOutputStream();
// 收發數據...
} catch (IOException e) {
} finally {
// 關閉順序:流先於連接
if (os != null) try { os.close(); } catch (IOException ignored) {}
if (is != null) try { is.close(); } catch (IOException ignored) {}
if (socket != null) try { socket.close(); } catch (IOException ignored) {}
}
3. TCP Socket 服務端 (需簽名權限/特定設備支持)
ServerSocketConnection server = null;
try {
server = (ServerSocketConnection) Connector.open("socket://:6666");
while (running) {
SocketConnection client = (SocketConnection) server.acceptAndOpen();
// 建議:為每個客戶端啟動獨立線程處理
new Thread(new ClientHandler(client)).start();
}
} catch (IOException e) {
} finally {
if (server != null) try { server.close(); } catch (IOException ignored) {}
}
4. UDP 數據報通信
UDPDatagramConnection udp = null;
try {
udp = (UDPDatagramConnection) Connector.open("datagram://:5555"); // 綁定本地端口
Datagram dg = udp.newDatagram(1024); // 緩衝區大小
// 發送
String msg = "HELLO";
byte[] data = msg.getBytes();
Datagram snd = udp.newDatagram(data, data.length, "datagram://192.168.1.100:6666");
udp.send(snd);
// 接收
udp.receive(dg); // 阻塞
String rcv = new String(dg.getData(), 0, dg.getLength());
} catch (IOException e) {
} finally {
if (udp != null) try { udp.close(); } catch (IOException ignored) {}
}
資源管理鐵律
- finally 塊必須關閉:Connection、InputStream、OutputStream、Datagram
- 關閉順序:流 → 連接 (反初始化順序)
- 異常吞噬:關閉時的 IOException 通常可忽略,記錄日誌即可
- 超時設置:HttpConnection 無連接超時 API,需自行實現線程監控
MIDP 2.0 新增 HTTPS 支持
HttpsConnection 繼承 HttpConnection,增加 getSecurityInfo() 獲取證書/密鑰信息
- 設備必須內建根 CA 證書庫,自簽名證書需用戶確認安裝
- 握手過程顯著增加連接延遲和 CPU 開銷,權衡安全性與性能
設計哲學啟示
GCF 的接口分層、工廠解耦、URL 統一寻址,體現了面向接口編程、依賴倒置、最小知識原則。這種在極約束下的優雅設計,值得所有中間件/框架開發者深入研習。
- 上一篇文章: J2ME 網絡編程實戰:從 HTTP 通訊到網絡遊戲完整架構
- 下一篇文章: J2ME 2D 繪圖坐標系統變換:translate 平移與滾動背景實現
Copyright ©
程式碼之家 All Rights Reserved