I cant get my error message. When the connection is OK, i can see the message "Connection OK". But when i want to connect to a "bad" ip server, i cant see the message "Bad Connection". Why is this? I waited until 2 minutes (i read connection has 2 minutes timeout) but nothing happens.
我想做的是取消我的连接, 并显示一个消息是连接是坏的, 或者我无法从服务器获得信息 。
public void run() {
try {
conn = (HttpConnection) Connector.open(URL);
int status = conn.getResponseCode();
if (status != HttpConnection.HTTP_OK) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
mainScreen.add(new RichTextField("Bad Connection"));
}
});
conn.close();
return;
}
InputStream contentIn = conn.openInputStream();
byte[] data = new byte[400];
int length = 0;
StringBuffer raw = new StringBuffer();
while (-1 != (length = contentIn.read(data))) {
raw.append(new String(data, 0, length));
str = raw.toString();
conn.close();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
mainScreen.add(new RichTextField("Connection OK"));
}
});
}
} catch (Exception e) {
}
}