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