📄 httpexample_1.java
字号:
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
/*
* 获得指定网页的数据
*/
public class HttpExample_1 extends MIDlet implements CommandListener {
private Command exitCommand;
private TextBox tb;
String url = "http://127.0.0.1:8080/HelloWorld.html";
public HttpExample_1() {
exitCommand = new Command("Exit", Command.EXIT, 1);
tb = new TextBox("StreamConnectiont例子", "", 200, 0);
tb.addCommand(exitCommand);
tb.setCommandListener(this);
}
protected void startApp() {
try {
StreamConnection c = null;
InputStream s = null;
StringBuffer b = new StringBuffer();
try {
//使用StreamConnection接口
c = (StreamConnection) Connector.open(url);
s = c.openInputStream();
int ch;
while ((ch = s.read()) != -1) {
b.append((char) ch);
}
System.out.println(b.toString());
} finally {
if (s != null) {
s.close();
}
if (c != null) {
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 + -