国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

J2ME學習筆記(6)—連接MIDlet到文本文件

2019-11-17 06:23:46
字體:
來源:轉載
供稿:網友

  1. J2ME中的連接類
  1) J2ME中,網絡連接由類屬連接框架(Generic Connection Framework)(GCF)處理,它是一組API,它有一個類和八個接口。GCF駐留在javax.microedition.io包中。
  
  2) CGF的優點:
  
  增加了支持不同種類網絡協議的一致性;
  
  定義和使用更可靠和可擴充的新協議;
  
  增加與標準Java技術中類庫的兼容性。
  
  3) GCF包括
  
  類(Connector)
  
  異常(ConnectionNotFoundException)
  
  接口(Connection,DatagramConnection,StreamConnectionNotifier,InputConnection,InputConnection,StreamConnection,ContentConnection)
  
  4) J2ME中用microedition.io包替代J2SE中java.net包。
  
  2. J2ME中的I/O類
  1) J2ME中I/O類庫由java.io包支持
  
  2) 例如使用Reader和Writer類處理字符流
  
    使用InputerStream和OutputStream類處理字節流
  
  3. 實例:
  1) 任務陳述:SaveMyMoney銀行應用程序需要對存儲在J2EE服務器上的文本文件進行檢索,并在手機屏幕上隨機顯示文本中某一行的內容
  
  2) 開發步驟:
  
  a.打開記事本,寫如如下代碼:
  
  The keyWord to know the current balance is SMMBCBAL.
  
  The keyword to know the check status is SMMBCHKS.
  
  The keyword to oBTain mini statement is SMMBMINI
  
  The keyword to know the fixed deposit details is SMMBFDDT.
  
  The keyword to request for checkbook is SMMBBOOK.
  
  The keyword to stop check transaction is SMMBSTOP.
  
  The keyword to request for bill PResentation is SMMBBILL.
  
  The keyword to request for help is SMMBHELP.
  
  保存為keyword.txt,并把該文件放入J2EE服務器的public_Html文件夾中,作為SaveMyMoney銀行應用程序檢索的對象。(前提是你必須安裝J2EE服務器,你可以查看J2EE相關資料)。
  
  b.編寫代碼,如下:
  
  import javax.microedition.midlet.*;
  
  import javax.microedition.lcdui.*;
  
  import java.io.*;
  
  //javax.microedition.io包包含用來把MIDlet連接到網絡資源上所要使用的類和接口,
  
  //假如你要建立MIDlet和文本文件之間的雙向連接,你可以使用StreamConnection接口。
  
  //你可用HTTP連接來檢索儲存在J2EE服務器中的文本文件的數據
  
  import javax.microedition.io.*;
  
  import java.util.*;
  
   public class SaveMyMoney extends MIDlet implements
  
  CommandListener
  
   {
  
    private Command exitCommand, nextCommand;
  
    private Display display;
  
    private Form form;
  
    private StringItem keyWord;
  
    private Vector keyVector;
  
    public SaveMyMoney()
  
    {
  
    display = Display.getDisplay(this);
  
    exitCommand = new Command("Exit",Command.EXIT,2);
  
    nextCommand = new Command("Next",Command.OK,2);
  
    form = new Form("SMMB KEYWORDS HELP");
  
    keyWord = new StringItem(" ","we help");
  
    Ticker ticker = new Ticker("want to know your balance, check status ,transaction details, or bill details?use these keywords to bank with us");
  
    form.setTicker(ticker);
  
    form.append(keyWord);
  
    form.addCommand(exitCommand);
  
    form.addCommand(nextCommand);
  
    form.setCommandListener(this);
  
    keyVector = new Vector();
  
    }
  
   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==exitCommand)
  
    {
  
    destroyApp(false);
  
    notifyDestroyed();
  
    }
  
    else if(c==nextCommand)
  
    {
  
    showKeyword();
  
    }
  
   }
  
   private void readKeyword()
  
   {
  
    StreamConnection connect = null;
  
    //創建輸入流以檢索連接中的數據
  
    InputStream inStream = null;
  
    //創建一個存儲被檢索數據的串緩沖區
  
    StringBuffer buffer = new StringBuffer();
  
    try
  
     {
  
         //建立HTTP與存儲在J2EE服務器中的keyword.txt文件進行連接
  
         //Connection對象被設置為StreamConnection類型,以便輸入和輸出流可通過連接發送.
  
      connect = (StreamConnection)Connector.open("http://localhost:8000/keyword.txt");
  
      //openInputStream方法打開連接的輸入流
  
      inStream = connect.openInputStream();
  
      int input;
  
      //用read()方法檢索數據,返回-1時到達文本文件末尾,while循環終止
  
      while ((input=inStream.read())!= -1)
  
      {
  
         //緩沖區一次存儲一行文本
  
      if (input!='/n')
  
      {
  
       buffer.append((char)input);
  
      }
  
      else
  
      {
  
      //文本被傳遞到向量keyVector
  
       keyVector.addElement(buffer.toString());
  
       buffer = new StringBuffer();
  
      }
  
      }
  
     }
  
    catch(IOException e)
  
    {
  
    System.err.println(" the connection could not be established. sorry for the inconvenience");
  
    }
  
   }
  
   private void showKeyword()
  
   {
  
    //隨機地從向量中選擇一行文本,把此行存儲在稱為keyword的StringItem對象中,然后在手機屏幕上顯示此行
  
    Random random = new Random(Calendar.getInstance().getTime().getTime());
  
    int position = Math.abs(random.nextInt()) % keyVector.size();
  
    keyWord.setText((String)keyVector.elementAt(position));
  
   }
  
  }
  
  c.運行J2EE服務器,在命令提示符下打入命令j2ee –verbose
  
  d.打開Ktoolbar,新建項目----點擊Build進行編譯,預檢驗和打包----點擊Run進行測試

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿州市| 连江县| 云浮市| 湖北省| 元谋县| 夏河县| 五大连池市| 南召县| 无锡市| 加查县| 河曲县| 秭归县| 莫力| 涟水县| 高邮市| 兴化市| 乌拉特后旗| 丘北县| 祁阳县| 定边县| 长宁区| 毕节市| 定南县| 离岛区| 集安市| 日土县| 什邡市| 阿克陶县| 天水市| 乐安县| 泸州市| 丽水市| 大兴区| 册亨县| 嘉峪关市| 图木舒克市| 扎鲁特旗| 台北市| 呼玛县| 张北县| 双辽市|