日期:2014-05-20 浏览次数:20997 次
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
//送给服务器一些信息,得到响应,例如:
public class MIDlet3 extends MIDlet implements CommandListener{
private Form frm = new Form("HTTP测试");
private TextField tfAcc = new TextField("输入账号","",10,TextField.ANY);
private TextField tfPass = new TextField("输入账号","",10,TextField.ANY);
private Command cmdLogin = new Command("登陆",Command.SCREEN,1);
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(frm);
frm.append(tfAcc);
frm.append(tfPass);
frm.addCommand(cmdLogin);
frm.setCommandListener(this);
}
public void commandAction(Command c,Displayable d){
if(c==cmdLogin){
ValidateTehread vt = new ValidateTehread();
vt.start();
}
}
class ValidateTehread extends Thread{
public void run(){
try{
String url = "http://localhost:9999/login.jsp?acc="+tfAcc.getString()+"&pass="+tfPass.getString();
//连接到HTTP服务器
HttpConnection hc =
(HttpConnection)Connector.open(url);
DataInputStream dis = hc.openDataInputStream();
byte[] b = new byte[(int)hc.getLength()];
dis.read(b);
frm.append(new String(b));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}