首先,在没有使用后台线程控制HTTP连接时,常常会造成手机系统死机的假象。
方法一:
主程序中直接使用后台线程:
可以设计一个网络连接按钮connectCommand,当用户按下该按钮的时候,才开始进入网络连接状态。
但是J2ME不能够在commandAction()方法中直接进行网络连接,因此需要创建一个后台网络连接的线程。
如下:
1: public void commandAction(Command c, Displayable d) {
2: if (c == connectCommand) {//按下连接按钮,则启动一个后台线程
3: Thread t = new Thread(){
4: public void run(){
5: //进行网络连接
6:
7: }
8: };
9: t.start();
10: }
11: if (c == exitCommand) {
12: try {
13: destroyApp(false);
14: notifyDestroyed();
15: } catch (MIDletStateChangeException e) {
16: e.printStackTrace();
17: }
18: }
19: }
方法二:
主程序中调用后台线程子类
单独创建一个类来实现网络连接,这个类可以继承Thread 也可以继承Runnable,然后这个类处理各种网络连接以后来实现它的功能。使用的基本方法:
1: public void commandAction(Command c, Displayable d) {
2: if (c == okCommand) {
3: Logger f = new Logger (this,getString());
4: Thread t =new Thread(f);
5: t.start();//启动线程
6: display.setCurrent(f);
7: }
8: }
9:
10: class Logger extends Form implements Runnable,CommandListener{
11: Logger (Displayable next,String url){
12: }
13: }
下面是完整的程序:
1: import javax.microedition.io.Connector;
2: import javax.microedition.io.HttpConnection;
3: import javax.microedition.lcdui.Alert;
4: import javax.microedition.lcdui.Command;
5: import javax.microedition.lcdui.CommandListener;
6: import javax.microedition.lcdui.Display;
7: import javax.microedition.lcdui.Displayable;
8: import javax.microedition.lcdui.Form;
9: import javax.microedition.lcdui.StringItem;
10: import javax.microedition.lcdui.TextBox;
11: import javax.microedition.midlet.MIDlet;
12: import javax.microedition.midlet.MIDletStateChangeException;
13:
14: public class HttpLogger extends MIDlet {
15: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
16: private Command okCommand = new Command("OK", Command.OK, 1);
17: private Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
18: private Display display;
19: private URLEntry mainForm;
20:
21: public HttpLogger() {
22: }
23:
24: protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
25: exitMIDlet();
26: }
27:
28: protected void pauseApp() {
29: // TODO Auto-generated method stub
30:
31: }
32:
33: protected void startApp() throws MIDletStateChangeException {
34: if (display == null) {
35: display = Display.getDisplay(this);
36: mainForm = new URLEntry();
37: // 显示主屏幕
38: display.setCurrent(mainForm);
39: }
40: }
41:
42: // 退出程序
43: private void exitMIDlet() {
44: notifyDestroyed();
45: }
46:
47: // 显示错误信息
48: void displayError(Throwable e, Displayable next) {
49: Alert a = new Alert("错误");
50: a.setString(e.toString());
51: a.setTimeout(Alert.FOREVER);
52: display.setCurrent(a, next);
53: }
54:
55: // 输入URL,内部类
56: class URLEntry extends TextBox implements CommandListener {
57:
58: public URLEntry() {
59: super("Enter URL", "http://127.0.0.1:8080/HelloWorld.html", 100, 0);
60: addCommand(exitCommand);
61: addCommand(okCommand);
62: setCommandListener(this);
63: }
64:
65: public void commandAction(Command c, Displayable d) {
66: if (c == exitCommand) {
67: exitMIDlet();
68: } else if (c == okCommand) {
69: // 创建后台的网络连接
70: Logger f = new Logger(this, getString());
71: Thread t = new Thread(f);
72: t.start();
73: display.setCurrent(f);
74: }
75: }
76: }
77:
78: // 连接网络的新线程,内部类
79: class Logger extends Form implements Runnable, CommandListener {
80:
81: private String url;
82: private Displayable next;
83: private StringItem text = new StringItem(null, "");
84: private HttpConnection conn;
85:
86: public Logger(Displayable next, String url) {
87: super("HTTP 信息");
88: // 获得地址信息
89: this.url = url;
90: this.next = next;
91:
92: addCommand(cancelCommand);
93: setCommandListener(this);
94: // 屏幕上使用StringItem来显示信息
95: append(text);
96: }
97:
98: public void run() {
99: // 创建一个连接不要放在构造函数中
100: try {
101: conn = (HttpConnection) Connector.open(url);
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: // 屏幕上显示服务器上的信息
106: update("连接到" + conn.getURL());
107: try {
108: int rc = conn.getResponseCode();
109: update("Response code" + rc);
110: update("Response message:" + conn.getResponseMessage());
111: String key;
112: for (int i = 0; (key = conn.getHeaderFieldKey(i)) != null; ++i) {
113: // 获得网页Head信息
114: update(key + ":" + conn.getHeaderField(i));
115: }
116: } catch (Exception e) {
117: update("获得的异常:" + e.toString());
118: }
119: // 取消cancelCommand按钮
120: removeCommand(cancelCommand);
121: addCommand(okCommand);
122: }
123:
124: // 使用String item 显示获得的信息
125: private void update(String line) {
126: if (display.getCurrent() != this) {
127: return;
128: }
129: String old = text.getText();
130: StringBuffer buf = new StringBuffer();
131:
132: buf.append(old);
133: if (old.length() > 0) {
134: buf.append('/n');
135: }
136: buf.append(line);
137: text.setText(buf.toString());
138: }
139:
140: public void commandAction(Command arg0, Displayable arg1) {
141: display.setCurrent(next);
142: try {
143: conn.close();// 关闭网络连接
144: } catch (Exception e) {
145: displayError(e, next);
146: }
147: }
148: }
149: }
150: