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 {
padding-bottom: 0px !important; line-height: 18px; background-color: #f7f7ff !important; margin: 0em; padding-left:
分享到:
相关推荐
J2ME: The Complete Reference
This book targets all mobile software developers and business decision makers. It focuses on end-to-end architectural patterns, best practices, key innovations, and real-world design approaches.
我整理生成的CHM格式文件 J2ME:循序渐进.chm 因为最近在搞这个所以整理了一些资料和大家分享 并非本人原创,郑重感谢原作者!
J2ME的MIDP在javax.microedition.io程序包中提供了一整套类,统称为通用连接构架(Generic Connection Framework ,GCF)。GCF使得与其它设备的可编程传输(programmatically transferring)数据变得非常容易。
关于J2ME应用中一些多线程的使用方法和示例
J2ME里面的线程和网络 J2ME里面的线程和网络 J2ME里面的线程和网络
我们首先从J2ME GUI基础编程开始( javax.microedition.lcdui类),然后讨论数据存储和无线连接方面的问题。通过这一系列的文章,我会为读者提供一个示范性的Expenses MIDlet程序,这个小软件的主要作用是帮助用户在...
为了利用MIDP(Mobile Information Device Profile:移动信息设备框架)向读者介绍J2ME的开发知识,我将引领读者了解和掌握一个完整J2ME应用程序的创建过程,这一程序在本系列文章中都被命名为Expenses。在上一篇...
Sun推出的Java 2 Micro Edition (J2ME) 可以让开发人员创建专用于各类手持设备的应用程序,比如传统的PDA到所谓的智能电话等。为了利用MIDP(Mobile Information Device Profile:移动信息设备框架)向读者介绍J2ME...
分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME...
j2me使用udp的简单连接源码 server与Client的连接
第一次运行JB2006开如J2ME时出现 ALERT: java/lang/ClassFormatError: Bad version information.错误 点“启动”就会出错 我现在找到了答案 请你们下去看
j2me教程,比较基础,容易掌握,很容易上手,对初学者很有帮助。。
j2me写的秒表程序,使用多线程,对于初学者是一个很好的例子。。。
J2me中对HTTP 1.2中定义的KEEPALIVE支持得并不是很好,并不能获得一个http的长连接。对于JSE中http connection的实现还没验证。 使用Socket的一个好处是可以避免为每个网络请求建立单独的Connection。并且因为...
j2me入门:J2ME程序开发新手入门九大要点
J2ME API J2ME API J2ME API J2ME API J2ME API
J2ME源码:连连看 看到了就快下载吧,我不希望由于你的没下载而后悔!
j2me里面各种漂亮进度条的制作方法,例子和简洁哦~~