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

在無線 J2ME 設備上實現 HTTP 協議:GET 與 POST 請求完整指南

     編輯:J2ME

前言:移動網絡時代的 Java 機遇

隨著手機與 PDA 普及,從移動設備訪問 Web 服務已成剛需。Java 憑藉輕量級特性,成為開發此類應用的首選語言。本文將詳細講解如何在 J2ME/MIDP 環境下,利用 javax.microedition.io 套件發送 HTTP GET 與 POST 請求,並附上完整可運行的 MIDlet 範例碼。

核心架構:通用連接框架 (GCF)

J2SE 提供龐大的 java.net 庫,但對資源受限的無線設備並不適用。J2ME 定義了子集 —— 通用連接框架 (Generic Connection Framework, GCF),位於 javax.microedition.io。GCF 以 Connection 介面為核心,衍生出多種子介面。雖然廠商可選擇性實作,但 MIDP 強制要求支援 HttpConnection(繼承自 ContentConnection),確保跨設備可移植性。

發送 HTTP GET 請求

GET 請求流程:開啟連結 → 取得輸入串流 → 逐字元讀取回應 → 關閉資源。關鍵在於使用 Connector.open(url) 並轉型為 HttpConnection。

public String sendHttpGet(String url) throws IOException {
    HttpConnection hcon = null;
    DataInputStream dis = null;
    StringBuffer response = new StringBuffer();
    try {
        hcon = (HttpConnection) Connector.open(url);
        dis = new DataInputStream(hcon.openInputStream());
        int ch;
        while ((ch = dis.read()) != -1) {
            response.append((char) ch);
        }
    } finally {
        if (hcon != null) hcon.close();
        if (dis != null) dis.close();
    }
    return response.toString();
}

發送 HTTP POST 請求

POST 請求須指定讀寫模式、設定請求方法為 POST,並透過 DataOutputStream 寫入請求主體。注意:不要主動呼叫 flush(),否則某些手機會產生 Transfer-Encoding: chunked 導致異常;改由 openInputStream() 自動推送緩衝區。

public String sendHttpPost(String url, String requestBody) throws IOException {
    HttpConnection hcon = null;
    DataInputStream dis = null;
    DataOutputStream dos = null;
    StringBuffer response = new StringBuffer();
    try {
        hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
        hcon.setRequestMethod(HttpConnection.POST);
        dos = hcon.openDataOutputStream();
        byte[] data = requestBody.getBytes();
        for (int i = 0; i < data.length; i++) {
            dos.writeByte(data[i]);
        }
        // 不要 dos.flush();
        dis = new DataInputStream(hcon.openInputStream());
        int ch;
        while ((ch = dis.read()) != -1) {
            response.append((char) ch);
        }
    } finally {
        if (hcon != null) hcon.close();
        if (dis != null) dis.close();
        if (dos != null) dos.close();
    }
    return response.toString();
}

完整 MIDlet 範例:HttpMidlet

範例提供圖形介面讓使用者輸入 URL、選擇 GET/POST、顯示伺服器回應。核心邏輯封裝於 sendHttpGet 與 sendHttpPost 方法中,並在獨立執行緒中呼叫以避免阻塞 UI。

// 關鍵片段:commandAction 中的執行緒處理
if (disp == list && com == List.SELECT_COMMAND) {
    String result;
    if (list.getSelectedIndex() == 0) {
        result = sendHttpGet(requestField.getString());
    } else {
        result = sendHttpPost(requestField.getString());
    }
    resultField = new StringItem(null, result);
    resultScreen.append(resultField);
    myDisplay.setCurrent(resultScreen);
}

總結

HTTP 是所有 MIDP 設備唯一強制支援的網絡協議,掌握 GCF 與 HttpConnection 是開發無線網絡應用的基石。隨著無線設備普及,掌握此技術的開發者將迎來廣闊機遇。

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