嗯,从上周五开始安装JAVA虚拟机,到今天总共6天,终于能够实现我想要的功能,呵呵
头一次接触JAVA,j2me,java语法都不会,还好有点c、c++基础
下面就做一下总结,防止以后忘了。。。。。
一 基本术语与常识
JDK-- java development kit :JAVA虚拟机,也就是说所有的java程序必须在jdk安装的前提下才能运行
J2ME--java 2.0 micro edition :应用于移动手机 pda
J2EE--java 2.0 enterprise edition:应用与服务器
J2SE--java 2.0 standard edition:标准规范
WTK--wireless tools kit
MIDP--mobile information device profile
eclipse--java开发环境
--------------------------------------------------------------
j2me环境搭建:
参考http://www.j2megame.org/index.php/content/view/1336/140.html
http://ouycp.blog.163.com/blog/static/2694739720073553822198/
--------------------------------------------------------------
我实现的功能手机客户端向服务器发送socket指令,然后服务器返回相应的图像
/*****************/
/*author:kevin
date:2009-5-27
name:sockeimageclient.java
copyright:all reserved
*/
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
display=Display.getDisplay(this);
mainForm=new Form("Image Getter");
mainForm.append("click to get Image");
mainForm.addCommand(connectCommand);
mainForm.addCommand(exitCommand);
mainForm.append(tf);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
gt=new GetterThread(this);
gt.start();
}
public void setImage(Image image){
mainForm.append(image);
display.setCurrent(mainForm);
}
public void commandAction(Command cmd, Displayable d) {
// TODO 自动生成方法存根
if(cmd==connectCommand){
synchronized(this){
notify();
}
}
else if(cmd==exitCommand){
exitMIDlet();
}
}
private void exitMIDlet(){
try{
destroyApp(false);
notifyDestroyed();
}catch (MIDletStateChangeException e){
e.printStackTrace();
}
}
class GetterThread extends Thread{
private socketimageclient midlet;
public static final String URL="socket://192.168.1.168:8888";
private SocketConnection sc=null;
private DataInputStream dis=null;
private DataOutputStream dos=null;
public GetterThread(socketimageclient midlet){
this.midlet=midlet;
}
public void run(){
synchronized(midlet){
try{
midlet.wait();
try {
sc=(SocketConnection)Connector.open(URL);
dos=sc.openDataOutputStream();
String tfs=tf.getString();
dos.writeUTF(tfs);
} catch (IOException e) {
e.printStackTrace();
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println("Connect to server...");
try{
// sc=(SocketConnection)Connector.open(URL);
dis=sc.openDataInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = dis.read()) != -1)
{
baos.write(ch);
}
byte[] imageData = baos.toByteArray();
Image image=Image.createImage(imageData,0,imageData.length);
midlet.setImage(image);
baos.close();
dis.close();
sc.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}