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

MIDP 通訊 API 深度解析:HttpConnection 與外部系統整合

     編輯:J2ME

本文為 MIDP 系列最後篇,深入探討 javax.microedition.io 套件如何讓 MIDlet 與外部系統(Web 伺服器、Servlet、JSP)進行資料交換。以完整的投票系統為例,展示 HTTP GET/POST、XML 解析、MVC 架構在資源受限環境下的實作。

Connection 階層架構

所有連線介面繼承自 Connection:

  • HttpConnection — HTTP/HTTPS 協定(MIDP 強制支援)
  • StreamConnection — TCP Socket 串流
  • DatagramConnection — UDP 資料包
  • CommConnection — 序列埠
  • FileConnection — 檔案系統 (JSR 75)

HttpConnection 關鍵方法

// 請求設定
void setRequestMethod(String method); // GET/POST
void setRequestProperty(String key, String value);

// 回應取得
int getResponseCode();
String getResponseMessage();
String getHeaderField(String name);
long getHeaderFieldDate(String name, long def);
int getHeaderFieldInt(String name, int def);
long getLength(); // Content-Length
String getType(); // Content-Type
String getURL();

// 串流存取
InputStream openInputStream();
OutputStream openOutputStream();

Connector 工廠模式

根據 URL 協定前綴自動建立對應連線:

協定URL 範例回傳介面
HTTPhttps://example.com/apiHttpConnection
HTTPShttps://example.com/apiHttpsConnection
Socketsocket://host:portStreamConnection
Datagram (客戶端)datagram://host:portDatagramConnection
Datagram (伺服器)datagram://:portDatagramConnection

完整案例:投票系統

架構概覽

  • Model: VoteSummary (投票統計)、Vote (單一選項)
  • View: VoteResults 繼承 Form 顯示結果
  • Controller: VoteResults 實作 CommandListener 處理使用者投票
  • Network: ResourceUtility 封裝 HTTP 通訊與 XML 解析

網路通訊核心:ResourceUtility

public class ResourceUtility {
    private static final String VOTE_URL = "https://server/vote.jsp";

    public static VoteSummary getVoteSummary() throws IOException {
        String xml = backendComms(VOTE_URL, "");
        return XMLUtil.parseVoteSummary(xml);
    }

    public static VoteSummary submitVote(Vote vote) throws IOException {
        String params = "vote=" + vote.getValue();
        String xml = backendComms(VOTE_URL, params);
        return XMLUtil.parseVoteSummary(xml);
    }

    private static String backendComms(String url, String params) throws IOException {
        HttpConnection conn = null;
        DataInputStream dis = null;
        try {
            String fullUrl = url + (params.isEmpty() ? "" : "?" + params);
            conn = (HttpConnection) Connector.open(fullUrl, Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.GET);
            
            dis = new DataInputStream(conn.openInputStream());
            StringBuffer sb = new StringBuffer();
            int ch;
            long len = conn.getLength();
            if (len > 0) {
                for (int i = 0; i < len; i++) {
                    if ((ch = dis.read()) != -1) sb.append((char)ch);
                }
            } else {
                while ((ch = dis.read()) != -1) sb.append((char)ch);
            }
            return sb.toString();
        } finally {
            closeQuietly(dis);
            closeQuietly(conn);
        }
    }
}

XML 解析:kXML 輕量級解析器

public class XMLUtil {
    public static VoteSummary parseVoteSummary(String xml) throws IOException {
        InputStreamReader reader = new InputStreamReader(
            new ByteArrayInputStream(xml.getBytes()));
        
        XmlParser parser = new XmlParser(reader);
        Document doc = new Document();
        doc.parse(parser);
        
        Element root = doc.getElement("vote-example");
        Element votes = root.getElement("votes");
        
        String total = getText(votes, "number");
        String avg = getText(votes, "average");
        
        Element choices = votes.getElement("choices");
        Vector list = new Vector<>();
        for (int i = 0; i < choices.getChildCount(); i++) {
            if (choices.getType(i) == Xml.ELEMENT) {
                Element choice = choices.getElement(i);
                String val = getText(choice, "value");
                String name = getText(choice, "name");
                String cnt = getText(choice, "number");
                list.addElement(new Vote(val, name, cnt));
            }
        }
        return new VoteSummary(total, avg, list);
    }
    
    private static String getText(Element parent, String name) {
        return parent.getElement(name).getText();
    }
}

JSP 後端 (vote.jsp)

<%@ page contentType="text/xml; charset=UTF-8" %>
<%
    // 簡易記憶體儲存,實際應用請用資料庫
    Map votes = (Map) application.getAttribute("votes");
    if (votes == null) { votes = new HashMap<>(); application.setAttribute("votes", votes); }
    
    String v = request.getParameter("vote");
    if (v != null) votes.put(v, votes.getOrDefault(v, 0) + 1);
    
    int total = votes.values().stream().mapToInt(Integer::intValue).sum();
    double avg = choices.stream().mapToInt(c -> c.value * votes.getOrDefault(c.key,0)).average().orElse(0);
%>

  
    <%= total %>
    <%= avg %>
    
      1Option A<%= votes.getOrDefault("1",0) %>
      2Option B<%= votes.getOrDefault("2",0) %>
    
  

MIDlet 整合

public class VoterMIDlet extends MIDlet implements ScreenCallback {
    private VoteResults voteScreen;

    protected void startApp() {
        if (voteScreen == null) {
            voteScreen = new VoteResults(this);
            new Thread() {
                public void run() {
                    try {
                        VoteSummary summary = ResourceUtility.getVoteSummary();
                        voteScreen.update(summary);
                    } catch (IOException e) {
                        voteScreen.showError("載入失敗: " + e.getMessage());
                    }
                }
            }.start();
        }
        Display.getDisplay(this).setCurrent(voteScreen);
    }
    // ... ScreenCallback.exit() 實作
}

離線運作與同步化

MIDP 應用可在無網路覆蓋時離線運作(讀取本地 RMS 資料),恢復連線後同步資料至伺服器。此特性優於 WAP,適合郵件用戶端、離線地圖、待辦清單等應用。

HTTPS 支援現況

早期 WTK 支援 HTTPS,但正式版曾移除。現代 Java ME 平台多已恢復支援,可實現金融交易、安全登入等需求。

MVC 在 MIDP 的實踐要點

  • Model (VoteSummary) 純資料類別,無 UI 依賴
  • View/Controller 合一 (VoteResults) 減少類別數量
  • 網路層 (ResourceUtility) 獨立封裝,可複用
  • XML 解析 (XMLUtil) 隔離協定細節
  • 使用介面 (ScreenCallback) 解耦 MIDlet 與 UI

這套架構展示了在嚴格資源限制下,如何建構清晰、可維護、可擴展的網路應用程式。

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