package http;
import java.io.DataInputStream;
import java.io.IOException;
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 HttpServlet2 extends MIDlet implements CommandListener{
private Display dis;
private Form frm=new Form("网页信息为"); //记住new
private TextField tf=new TextField("输入地址","",50,TextField.ANY);
private Command submit=new Command("登陆",Command.SCREEN,1);
public HttpServlet2() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
dis=Display.getDisplay(this);
dis.setCurrent(frm);
frm.addCommand(submit);
frm.append(tf);
frm.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==submit){
// String url=tf.getString(); //获取登陆的url
// "http://localhost:8080/lbs/mobile_terminal?"
// +"TS==1234567891&UserID=10226&StartIndex=1&Count=5"
// url+="mobile_terminal?TS==1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5";
try {
HttpConnection hc=(HttpConnection)Connector.open("http://localhost:8080/lbs/mobile_terminal?TS=1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5");
System.out.println(hc.getResponseCode());
DataInputStream di=hc.openDataInputStream();
int length=(int) hc.getLength(); //读servlet居然是负数
// byte [] b= new byte[length];
System.out.println(length);
byte [] b= new byte[1000];
di.read(b);
String str=new String(b);
System.out.print(str);
frm.append(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
以上代码不能执行,可能造成死锁
解决办法
改用线程
package http;
import java.io.DataInputStream;
import java.io.IOException;
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 HttpServlet2 extends MIDlet implements CommandListener{
private Display dis;
private Form frm=new Form("网页信息为"); //记住new
private TextField tf=new TextField("输入地址","",50,TextField.ANY);
private Command submit=new Command("登陆",Command.SCREEN,1);
public HttpServlet2() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
dis=Display.getDisplay(this);
dis.setCurrent(frm);
frm.addCommand(submit);
frm.append(tf);
frm.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if(c==submit){
ThreadDemo td=new ThreadDemo();
td.start();
}
}
class ThreadDemo extends Thread{
public void run(){
try {
HttpConnection hc=(HttpConnection)Connector.open(tf.getString()+"mobile_terminal?TS=1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5");
System.out.println(hc.getResponseCode());
DataInputStream di=hc.openDataInputStream();
int length=(int) hc.getLength(); //读servlet居然是负数
System.out.println(length);
byte [] b= new byte[1000];
di.read(b);
String str=new String(b);
System.out.print(str);
frm.append(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}