移动Web服务项目解析
立即解锁
发布时间: 2025-08-17 01:03:23 阅读量: 4 订阅数: 7 

J2ME无线编程与应用开发指南
### 移动 Web 服务项目解析
在移动应用开发中,提供多样化的服务是吸引用户的关键。本文将详细解析一个移动 Web 服务项目,该项目包含多个功能模块,如天气查询、电影票务预订和新闻阅读等。我们将深入探讨项目中的各个类及其功能,包括 `MainClass`、`DataParser` 和 `Weather` 类。
#### 1. MainClass 类
`MainClass` 类是整个应用的入口点,负责启动移动服务并调用其他三个 MIDlets。以下是该类的主要功能和代码解析:
```java
// © 2001 Dreamtech Software India, Inc.
// All rights Reserved
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
public class MainClass extends MIDlet implements CommandListener {
private List menu = null;
private Display displaymenu = null;
MoviesList bookmovie = null;
News readnews = null;
Weather seeweather = null;
Command exit;
public MainClass() {
displaymenu = Display.getDisplay(this);
exit = new Command("EXIT", Command.EXIT, 1);
}
public void startApp() {
menu = new List("Menu", List.IMPLICIT);
menu.append("Weather", null);
menu.append("Movies", null);
menu.append("News", null);
menu.setCommandListener(this);
displaymenu.setCurrent(menu);
menu.addCommand(exit);
}
public void pauseApp() {
System.out.println("pause app called");
}
public void destroyApp(boolean b) {
menu = null;
displaymenu = null;
bookmovie = null;
readnews = null;
seeweather = null;
}
public void commandAction(Command c, Displayable d) {
if (c == List.SELECT_COMMAND) {
String menuselect = menu.getString(menu.getSelectedIndex());
if (menuselect.equals("Weather")) {
seeweather = new Weather(displaymenu, this);
seeweather.startApp();
}
if (menuselect.equals("Movies")) {
bookmovie = new MoviesList(displaymenu, this);
bookmovie.startApp();
}
if (menuselect.equals("News")) {
readnews = new News(displaymenu, this);
readnews.startApp();
}
}
if (c == exit) {
destroyApp(true);
notifyDestroyed();
}
}
public void getDisplayObject(Display dis) {
displaymenu = dis;
displaymenu.setCurrent(menu);
}
}
```
**代码解析**:
- **导入语句**:导入了运行该应用所需的类。
- **类声明**:`MainClass` 继承自 `MIDlet` 并实现了 `CommandListener` 接口。
- **对象声明**:声明了 `List`、`Display`、`MoviesList`、`News`、`Weather` 和 `Command` 等对象。
- **构造函数**:获取 `Display` 对象并初始化退出命令。
- **startApp 方法**:初始化一个列表,显示可用的服务,并注册事件处理。
- **pauseApp 方法**:在应用暂停时打印信息。
- **destroyApp 方法**:将所有对象设置为 `null`,释放资源。
- **commandAction 方法**:处理用户事件,根据用户选择调用相应的 MIDlet。
- **getDisplayObject 方法**:获取 `Display` 对象并显示菜单列表。
#### 2. DataParser 类
`DataParser` 类用于处理 HTTP 请求、读取和解析 XML 数据。该类为应用中的三个 MIDlets 提供服务。以下是该类的主要功能和代码解析:
```java
// © 2001 Dreamtech Software India, Inc.
// All rights Reserved
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.kxml.*;
import org.kxml.parser.XmlParser;
import org.kxml.parser.*;
import java.util.Vector;
import java.util.Hashtable;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.lang.String;
import javax.microedition.rms.*;
public class DataParser {
HttpConnection con = null;
InputStream ins = null;
ParseEvent event;
AbstractXmlParser parser;
Vector hall_id, hall_name, halldata, vnews;
Hashtable htable;
RecordStore recordStore = null;
public DataParser() {
hall_id = new Vector();
hall_name = new Vector();
halldata = new Vector();
vnews = new Vector();
htable = new Hashtable();
htable = new Hashtable();
}
public void sourceurl(String url) {
try {
con = (HttpConnection) Connector.open(url);
ins = con.openInputStream();
parser = new XmlParser(new InputStreamReader(ins));
} catch (IOException ex) {
System.out.println("IOException occured");
}
}
void read_category() {
boolean foundnews = false;
do {
try {
event = parser.read();
if (event.getType() == Xml.START_TAG) {
StartTag stag = (StartTag) event;
if (stag.getName().equals("category")) {
foundnews = true;
}
}
if (event.getType() == Xml.TEXT) {
TextEvent tevent = (TextEvent) event;
if (foundnews) {
vnews.addElement(tevent.getText());
foundnews = false;
}
}
} catch (IOException ex) {
System.out.println("Exception occured");
}
} while (!(event instanceof EndDocument));
}
void read_record() {
int k, id;
String state = "", data = "";
boolean founditem = false, foundstate = false;
try {
recordStore = RecordStore.openRecordStore("addresses", true);
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
do {
try {
event = parser.read();
if (event.getType() == Xml.START_TAG) {
StartTag stag = (StartTag) event;
if (stag.getName().equals("weather")) {
founditem = true;
}
if (stag.getName().equals("state")) {
foundstate = true;
}
}
if (event.getType() == Xml.TEXT) {
TextEvent tevent = (TextEvent) event;
if (foundstate) {
state = tevent.getText();
data = data + "?" + state;
foundstate = false;
} else {
data = data + "?" + tevent.getText();
}
}
if (event.getType() == Xml.END_TAG) {
EndTag etag = (EndTag) event;
if (etag.getName().equals("weather")) {
data = data + "?";
founditem = false;
id = record_add(data);
htable.put((Object) state, Integer.toString(id));
data = "";
}
}
} catch (IOException ex) {
System.out.println("Exception occured");
}
} while (!(event instanceof EndDocument));
try {
recordStore.closeRecordStore();
} catc
```
0
0
复制全文


