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

J2ME 學習筆記(六):GCF 網路連接與 HTTP 檢索文字檔案實作

     編輯:Java 程式設計入門

前言

J2ME 網路通訊建立在 Generic Connection Framework (GCF) 之上。本文介紹 GCF 核心類別、I/O 流處理,並以「SaveMyMoney 銀行關鍵字查詢」完整實作:從 J2EE 伺服器 HTTP 讀取文字檔、解析每行關鍵字存入 Vector、隨機顯示於手機畫面。

一、GCF 核心架構

  • 套件:javax.microedition.io (取代 J2SE java.net)。
  • 核心類:Connector (靜態 open() 建立連線)。
  • 例外:ConnectionNotFoundException。
  • 介面:Connection、StreamConnection、ContentConnection、DatagramConnection、StreamConnectionNotifier 等。
  • 優點:協定一致性、可擴充新協定、與 J2SE 類別庫相容。

二、I/O 流處理

  • 位元組流:InputStream/OutputStream (套件 java.io)。
  • 字元流:Reader/Writer。

三、完整實作步驟

1. 伺服器端準備

建立 keyword.txt 放置 J2EE 伺服器 public_html 目錄,內容每行一關鍵字說明。

2. MIDlet 程式碼

public class SaveMyMoney extends MIDlet implements CommandListener {
    private Display display;
    private Form form;
    private StringItem keyWord;
    private Vector keyVector = new Vector();
    private Command exitCmd, nextCmd;

    public SaveMyMoney() {
        display = Display.getDisplay(this);
        exitCmd = new Command("Exit", Command.EXIT, 2);
        nextCmd = new Command("Next", Command.OK, 2);
        form = new Form("SMMB KEYWORDS HELP");
        keyWord = new StringItem(" ", "we help");
        form.setTicker(new Ticker("want to know your balance... use these keywords to bank with us"));
        form.append(keyWord);
        form.addCommand(exitCmd);
        form.addCommand(nextCmd);
        form.setCommandListener(this);
    }

    public void startApp() throws MIDletStateChangeException {
        display.setCurrent(form);
        readKeyword();
        showKeyword();
    }

    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCmd) { destroyApp(false); notifyDestroyed(); }
        else if (c == nextCmd) { showKeyword(); }
    }

    private void readKeyword() {
        StreamConnection conn = null;
        InputStream in = null;
        StringBuffer buf = new StringBuffer();
        try {
            conn = (StreamConnection) Connector.open("https://localhost:8000/keyword.txt");
            in = conn.openInputStream();
            int ch;
            while ((ch = in.read()) != -1) {
                if (ch != ' ') { buf.append((char)ch); }
                else { keyVector.addElement(buf.toString()); buf = new StringBuffer(); }
            }
        } catch (IOException e) {
            System.err.println("Connection failed: " + e);
        } finally {
            try { if (in != null) in.close(); } catch (IOException ignored) {}
            try { if (conn != null) conn.close(); } catch (IOException ignored) {}
        }
    }

    private void showKeyword() {
        Random rnd = new Random(System.currentTimeMillis());
        int pos = Math.abs(rnd.nextInt()) % keyVector.size();
        keyWord.setText((String) keyVector.elementAt(pos));
    }
}

3. 執行步驟

  1. 啟動 J2EE 伺服器:j2ee -verbose
  2. KToolbar 建立專案,放入原始碼,Build → Run。

關鍵技術點總結

  • HTTP 連線使用 StreamConnection 雙向流。
  • 逐字元讀取並以空白鍵分隔行,適應簡單文字格式。
  • Vector 儲存動態行數,Random 實現隨機抽取。
  • 網路操作建議放入背景執行緒 (此範例為教學簡化置於主執行緒)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved