想写一个手机客户端发送信息的程序(socket) 求指导
一共两个界面 第一个界面输入IP和端口 点击连接 进入第二个界面 有点像QQ 在下面的对话框输入信息 上面的StirngItem列出客户端和服务器对话列表 现在界面都实现了 遇到的难题是不知道该把socket的建立连接和接受信息的线程写在哪个界面的类下面 我的代码是这样的 错误一大堆 希望大家能指导一下程序结构应该怎么弄 O(∩_∩)O谢谢!
==============================SocketMIDlet==============================
import
java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class SocketMIDlet extends MIDlet implements CommandListener
{
Display display = Display.getDisplay(this);
SocketScreen socketscreen = new SocketScreen();
ConnectScreen connectscreen;
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
notifyDestroyed();
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException
{
socketscreen.addCommand(new Command("退出", Command.EXIT, 1));
socketscreen.addCommand(new Command("连接", Command.OK, 1));
socketscreen.setCommandListener(this);
display.setCurrent(socketscreen);
}
public void commandAction(Command arg0, Displayable arg1)
{
if(arg0.getLabel().equals("连接"))
{
socketscreen.connect();
display.setCurrent(connectscreen = new ConnectScreen(socketscreen));
connectscreen.addCommand(new Command("发送", Command.OK, 1));
connectscreen.addCommand(new Command("返回", Command.BACK, 1));
connectscreen.setCommandListener(this);
}
else if(arg0.getLabel().equals("退出"))
{
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
else if(arg0.getLabel().equals("发送"))
{
try {
socketscreen.dos.writeUTF(connectscreen.diaf.getString());
} catch (
IOException e) {
e.printStackTrace();
}
connectscreen.send(connectscreen.diaf.getString());
connectscreen.diaf.setString("");
}
else if(arg0.getLabel().equals("返回"))
{
display.setCurrent(socketscreen);
}
}
}
==============================SocketScreen==============================
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
public class SocketScreen extends Form
{
SocketConnection connection = null;
DataInputStream dis = null;
DataOutputStream dos = null;
private String IP = "127.0.0.1";
private String port = "5688";
TextField ipf = new TextField("IP:", IP, 20, TextField.ANY);
TextField pof = new TextField("port:", port, 20, TextField.NUMERIC);
Ticker ticker =new Ticker("欢迎!");
public SocketScreen()
{
super("客户端");
append(ipf);
append(pof);
setTicker(ticker);
}
public void connect()
{
new Thread(new SocketConnector()).start();
}
class SocketConnector implements Runnable
{
public void run()
{
try {
connection = (SocketConnection)Connector.open("socket://"+IP+":"+port);
connection.setSocketOption(SocketConnection.LINGER, 5);
dis = connection.openDataInputStream();
dos = connection.openDataOutputStream();