
使用 Web Services 和数据绑定组件在你的手机上显示全球天气信息
本教程展示了如何使用 NetBeans IDE 中的 Java ME 数据绑定库和组件来创建一个 Java ME 客户端连接到一个 web 服务并显示返回的数据。该程序从一个在线的 web 服务中取得全球天气的信息并在 Sun Java Wireless Toolkit 模拟器中显示出来. 如果你还不知道如何使用 NetBeans 来开发 Java ME 程序, 在学习本教程前你应该先学习 NetBeans Java ME MIDP 开发快速入门 。
内容

完成本教程,你需要以下软件和资源:
创建手机客户端
在本部分,我们创建一个带有客户端 stubs 的手机程序来连接到一个全球天气信息 web 服务。
- 选择 “文件” > “新建项目”。在类别中选择“ Java ME”,在项目中选择 “Mobile 应用程序”, 如下图所示:

- 输入 global weatherBrowser 作为项目名称,去掉创建 HelloMIDlet 的小钩并点击“下一步”
- 选择 Sun Java Wireless Toolkit 2.5.2 为仿真器平台,保持其它默认.

点击“完成”. 项目窗口和编辑器将如下图所示.
生成 Mobile 客户端桩
在本部分我们要创建 Web 服务的客户端.
- 在项目窗口中的global weatherBrowser 项目节点上右键, 然后选择“新建” > “其它”. 在新建文件向导中选择 Java ME Web . 点击“下一步”.
- 在 WSDL URL 部分增加
http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL 然后点击 “检索 WDSL”. 保持"运行 Web Service“ 为默认选择.

- 钩上 "生成数据绑定结构" 然后点击完成. JSR - 172 stub已被创建.
- 在项目窗口的 CountryBrowser 项目节点上右键, 然后选择”新建“ > ”其它“. 在新建文件各层中选择 ”Java ME Web 服务客户端“. 点击"下一步”.
http://www.swanandmokashi.com/HomePage/WebServices/StockQuotes.asmx?WSDL
然后点击 “检索 WSDL”. 在该向导中的各个字段值都已自动地填好了, 如下所示:
点击“完成”.
IDE 生成客户端桩:
同时, 编辑显示如下视图:
- 选择“生成数据绑定结构”. 然后点击“生成桩" .
- 移动 HelloMIDlet 到 stockquotes 包. 重命名为 StockMIDlet. 删除 hello 包.
- 项目窗口显示如下项目结构:
你现在已经有你mobile 程序的骨架了. 在下一部分, 你需要设计你 Visual MIDlet 的布局.
设计可视化的 MIDlet
在本部分, 你将创建一个 visual MIDlet 和使用设计器增加组件并将它们关联起来. 最后布局将如下所示:
- a
- b
- c
- d
增加代码
所有以下代码要增加到你之前创建的 StockMIDlet .
- 通过增加 bind() 方法, 来创建客户的取得实时股票行情:
StockQuotes quotes = new StockQuotes_Stub(); String quotes_request = ""; for( int i = 0; i < getChoiceGroup().size(); i++ ) { if( getChoiceGroup().isSelected( i )) { quotes_request += getChoiceGroup().getString( i ) + " "; } } DataBinder.registerDataSet( quotes.GetStockQuotes( quotes_request ), "quotes" );
- 通过增加以下代码到 bind() 方法来绑定数据到 GUI 标签:
DataBinder.bind( "quotes.quote[index.index].companyName", new StringItemBindingProvider(), getStringItem(), StringItemBindingProvider.FEATURE_TEXT ); DataBinder.bind( "quotes.quote[index.index].openPrice", new StringItemBindingProvider(), getStringItem1(), StringItemBindingProvider.FEATURE_TEXT ); DataBinder.bind( "quotes.quote[index.index].dayHighPrice", new StringItemBindingProvider(), getStringItem2(), StringItemBindingProvider.FEATURE_TEXT ); DataBinder.bind( "quotes.quote[index.index].dayLowPrice", new StringItemBindingProvider(), getStringItem3(), StringItemBindingProvider.FEATURE_TEXT ); DataBinder.bind( "quotes.quote[index.index].stockQuote", new StringItemBindingProvider(), getStringItem4(), StringItemBindingProvider.FEATURE_TEXT ); DataBinder.bind( "quotes.quote[index.index].volume", new StringItemBindingProvider(), getStringItem5(), StringItemBindingProvider.FEATURE_TEXT );
- 让用户在多个股票之间切换:
private void next() { if (((Boolean) DataBinder.readValue("index.index < quotes.quote.length - 1")).booleanValue()) { DataBinder.writeValue("index.index", DataBinder.readValue("index.index + 1")); } }
private void prev() { if (((Boolean) DataBinder.readValue("index.index > 0")).booleanValue()) { DataBinder.writeValue("index.index", DataBinder.readValue("index.index - 1")); } }
- 为滚动增加索引:
private static final class QuotesIndexDS implements DataSet {
private static final String INDEX = "index"; private Long index = new Long(0);
public Class getType(String arg0) throws DataBindingException { return Integer.class; }
public Object getValue(String dataItemName) throws DataBindingException { if ("index".equals(dataItemName)) { return index; } throw new IllegalArgumentException("Invalid data item name"); }
public void setValue(String dataItemName, Object value) throws DataBindingException { if ("index".equals(dataItemName)) { index = (Long) value; DataBinder.fireDataSetChanged(this, INDEX); return; } throw new IllegalArgumentException("Invalid data item name"); }
public void setAsString(String dataItemName, String value) throws DataBindingException { if ("index".equals(dataItemName)) { index = new Long(Long.parseLong(value)); DataBinder.fireDataSetChanged(this, INDEX); return; } throw new IllegalArgumentException("Invalid data item name"); }
public boolean isReadOnly(String arg0) throws DataBindingException { return false; }
}
- 增加一个任务到等待屏幕, "simple cancellable", 增加 bind 方法:
public SimpleCancellableTask getTask() { if (task == null) { // write pre-init user code here task = new SimpleCancellableTask(); task.setExecutable(new org.netbeans.microedition.util.Executable() { public void execute() throws Exception { bind(); } }); // write post-init user code here } return task; }
- 在 IN 命令的方法里增加 next 和 prev 方法:
} else if (command == next) { // write pre-action user code here next();
// write post-action user code here } else if (command == prev) { // write pre-action user code here prev();
// write post-action user code here }
发布程序
在该项目上右键然后选择运行. 项目运行后会启动模拟器. 在模拟器屏幕点击“运行”按钮. 然后点击“选择”按钮. 程序会连接到互联网并返回最新的 Dilbert 数据, 如下所示:
跟下来的步骤
除了 IDE 内置的关于 Java ME 开发的帮助文档, 有关 Mobility 包技术的教程和文章可以在以下位置找到:
|
|