📄 httpexample_2.java
字号:
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
/*
* 获得指定网页的数据
*/
public class HttpExample_2 extends MIDlet implements CommandListener {
private Command exitCommand;
private TextBox tb;
String url = "http://127.0.0.1:8080/HelloWorld.html";
public HttpExample_2() {
exitCommand = new Command("Exit", Command.EXIT, 1);
tb = new TextBox("HttpConnectiont例子", "", 200, 0);
tb.addCommand(exitCommand);
tb.setCommandListener(this);
}
protected void startApp() {
try {
HttpConnection c = null;
InputStream s = null;
StringBuffer b = new StringBuffer();
try {
long len = 0;
int ch = 0;
c = (HttpConnection) Connector.open(url);
s = c.openInputStream();
len = c.getLength();
if (len != -1) {
// Read exactly Content-Length bytes
for (int i = 0; i < len; i++)
if ((ch = s.read()) != -1) {
b.append((char) ch);
} else {
//读取数据,直到网络断开为止.
while ((ch = s.read()) != -1) {
len = s.available();
b.append((char) ch);
}
}
}
} finally {
s.close();
c.close();
}
tb.setString(b.toString());
Display.getDisplay(this).setCurrent(tb);
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean d) {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -