在哪个地方加进线程,可以让返回的数据显示到屏幕上
/*
* PostDemo.java
*
* */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class PostDemo extends MIDlet
implements CommandListener {
private Display myDisplay = null;
private static String defaultURL =
"http://localhost:8084/HttpPOSTExampleServlet/ResponseLargeData";
private Form formMain;
private Form formResult;
private TextField requestField;
private StringItem resultField;
Command cmdSend = new Command("POST", Command.OK, 1);
Command cmdBack = new Command("返回", Command.OK, 1);
public PostDemo(){
myDisplay = Display.getDisplay(this);
formMain = new Form("Type in a URL:");
requestField =
new TextField(null, defaultURL, 100, TextField.URL);
formMain.append(requestField);
formMain.addCommand(cmdSend);
formMain.setCommandListener(this);
}
public void startApp() {
myDisplay.setCurrent(formMain);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
Display myDisplay = null;
formMain = null;
requestField = null;
formResult = null;
resultField = null;
}
public void commandAction(Command c, Displayable s) {
if (c == cmdSend) {
String urlstring = requestField.getString();
String resultstring = "";
try {
resultstring = sendPostRequest(urlstring);
} catch (IOException e) {
resultstring = "ERROR";
}
formResult = new Form("POST Result:");
resultField = new StringItem(null, resultstring);
formResult.append(resultField);
formResult.addCommand(cmdBack);
formResult.setCommandListener(this);
myDisplay.setCurrent(formResult);
} else if (c == cmdBack) {
requestField.setString(defaultURL);
myDisplay.setCurrent(formMain);
}
}
public String sendPostRequest(String urlstring) throws IOException {
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
InputStream is = null;
OutputStream os = null;
String message = "";
//向服务器发送的字符串
String requeststring = "test large data reading";
try {
hc = (HttpConnection)Connector.open(urlstring, Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
os = hc.openOutputStream();
System.out.println("hc.openOutputStream() 成功");
//往里写,向外读
byte[] request_body = requeststring.getBytes();
for (int i = 0; i < request_body.length; i++) {
os.write(request_body[i]);
System.out.println((char)request_body[i]);
}
os.flush();
System.out.println(" os.flush(); 成功\n");
int rc = hc.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP 响应代码: " + rc);
}
is = hc.openInputStream();
System.out.println(" hc.openInputStream() 成功");
System.out.println("\n从服务器读取数据...\n");
int size=0;
int ch;
while ((ch = is.read()) != -1) {
message = message + (char) ch;
size++;
System.out.print((char)ch);
}
message = "\n\n读取完成\n" +
"\n读取的字节数为:"+ size +"\n" + message;
System.out.println(message);
} finally {
if (hc != null) {
hc.close();
hc = null;
}
if (dis != null) {
dis.close();
dis = null;
}
if (dos != null) {
dos.close();
dos = null;
}
if (is != null) {
is.close();
is = null;
}
if (os != null) {
os.close();
os = null;
}
}
return message;
}
}
我想在if (c == cmdSend) 下面加个线程,post请求,可是 和下面的
formResult = new Form("POST Result:");
resultField = new StringItem(null, resultstring);
formResult.append(resultField);
formResult.addCommand(cmdBack);
formResult.setCommandListener(this);
myDisplay.setCurrent(formResult);
} else if (c == cmdBack) {
requestField.setString(defaultURL);
myDisplay.setCurrent(formMain);
就割断了 在哪个地方加求教了