13,100
社区成员
发帖
与我相关
我的任务
分享
import java.io.DataInputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDlet_Index extends MIDlet implements CommandListener {
private Display dis;
public String ReturnS = new String();
private Form frm = new Form("访问地址");
private Command cmdOK = new Command("确定", Command.SCREEN, 1);
private Form form = new Form("等待结果");
private StringItem si = new StringItem(null, "");
public MIDlet_Index() {
}
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(frm);
frm.addCommand(cmdOK);
frm.setCommandListener(this);//命令监听漏了
}
public void commandAction(Command c, Displayable d) {
if (c == cmdOK) {
ValidateThread vt = new ValidateThread();
vt.start();
// 先切换到另一界面,好看效果
dis.setCurrent(form);
form.append(si);
setText();// 赋值,线程有可能还没运行完,所以这值可能为空
}
}
private void setText() {
System.out.println("ReturnS=" + ReturnS);// 输出看看 注意两次输出
si.setText(ReturnS);
}
class ValidateThread extends Thread {
public void run() {
try {
String url = "http://192.168.255.56/MOA.nsf/Agent_MOA_Login";
HttpConnection hc = (HttpConnection) Connector.open(url);
DataInputStream dis = hc.openDataInputStream();
byte[] b = new byte[(int) hc.getLength()];
dis.read(b);
ReturnS = new String(b).trim();// 此刻获得到值了
setText();// 已经有值了,再次赋值
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}