WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院WEB开发Jsp J2ME学习笔记(6)—连接MIDlet到文本文件 阅读

J2ME学习笔记(6)—连接MIDlet到文本文件

 2008-01-05 19:41:02 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劕鎼痪褔鏌曟繝蹇曠窗闁煎壊浜滈—鍐偓锝庡墮娴犙勭箾閸喎鐏ユい鏇樺劦椤㈡瑩鎮℃惔銇帮拷
核心提示:1.J2ME中的连接类1)J2ME中,网络连接由类属连接框架(Generic Connection Framework)(GCF)处理,J2ME学习笔记(6)—连接MIDlet到文本文件,它是一组API,它有一个类和八个接口,然后在手机屏幕上显示此行Random random = newRandom(Calendar.

  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进行测试

Tags:JME 学习 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接