x;
private Display display = null;
private Form mainForm = null;
public static final Command exitCommand = new Command("Exit", Command.OK, 1);
public void startApp() {
Display.getDisplay(this).setCurrent(textbox);
if (display == null) {
display = Display.getDisplay(this);
}
mainForm = new Form("News Form");
// 从控制器加载
WbAction action = new WbAction();
String newsList = action.getNews();
mainForm.append(newsList);// 加载默认新闻标题列表
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void commandAction(Command cmd, Displayable displayable) {
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
基于J2ME的手机联网程序(3)
时间:2011-06-16
/***********************************************************/
package com.efan.wb.handle;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import com.efan.wb.dao.WbDao;
import com.efan.wb.model.UrlEntity;
public class WbAction {
public String getNews() {
// 从RMS中获取默认的URL
WbDao dao = new WbDao();
UrlEntity ue = dao.getDefaultURL();
String url = ue.getUrl();
WebExplorer we = new WebExplorer(url);
we.start();// 启动网络新闻获取线程
// 轮循等待操作完成
while (!we.isComplete()) {
// 超时处理,此略
}
return we.getNewsList();
}
// 为了简化代码,把这个访问网络的线程类作为内部类
class WebExplorer extends Thread {
private String newsList;
private String url;
public WebExplorer(String url) {
this.url = url;
}
public WebExplorer() {
}
public void setUrl(String url) {
this.url = url;
}
private boolean isComplete() {
return this.newsList == null ? false : true;
}
public String getNewsList() {
return newsList;
}
public void run() {
try {
HttpConnection conn = (HttpConnection) Connector.open(this.url);
DataInputStream is = conn.openDataInputStream();
this.newsList = is.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
基于J2ME的手机联网程序(4)
时间:2011-06-16
/***********************************************************/
package com.efan.wb.dao;
import com.efan.wb.model.UrlEntity;
public class WbDao {
public UrlEntity getDefaultURL() {
UrlEntity ue = new UrlEntity();
// 访问RMS,查询默认的URL,此略,直接硬编码来测试
String url = "http://localhost:8080/rss/news";
String name = "Test Web URL";
ue.setName(name);
ue.setUrl(url);
return ue;
}
}
/***********************************************************/
package com.efan.wb.model;
public class UrlEntity {
private String name;
private String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Stri
|