技术开发 频道

开发使用 Web 服务的、连接的 .NET Pocket PC 应用程序

 【IT168技术文档】Web 服务满足移动性

 Web 服务正在非常迅速地应用于主要系统集成开发过程中。在后台办公系统和旧式系统中,Web 服务已经主要用于提供一种机制,使得数据在长距离封闭边界之间和跨这些边界更自由地移动。整个行业已经加大力度,共同努力以便克服技术障碍(例如安全性)以及市场特定障碍(例如标准化的消息格式的需要)。

 继续进行这些努力的同时,也在迅速形成一个完全不同的市场:移动设备、软件和服务市场。于是,在交叉行业中出现了非常有趣的解决方案选项,其中 Web 服务满足移动性。Microsoft Windows .NET Compact Framework 为 Pocket PC 开启了 Web 服务的世界,而 Pocket PC 的无线特性又为 Web 服务开启了移动的世界。这意味着为扩展基于 XML 的 Web 服务提供商市场提供了新的动力。

 在本文中,我们将演练结合两种 Web 服务的示例 Pocket PC 项目的代码,这两种服务来自一家名为 ServiceObjects (http://www.serviceobjects.com/products/default.asp?bhcp=1) 的公司。

 按电话号码查找人员和企业

 动态实时服务 (DOTS) 是 ServiceObjects 公司对其商用 Web 服务的称呼。示例 Pocket PC 应用程序“People Anyplace”结合使用了 DOTS GeoPhone (http://www.serviceobjects.com/products/dots_geophone.asp) 和 DOTS YellowPages (http://www.serviceobjects.com/products/dots_yellowpages.asp),可以使用户轻松访问按电话号码排序的地址以及该地址附近的旅馆/餐馆。

 通过 DOTS GeoPhone XML Web 服务,可以访问超过 1.2 亿个链接有姓名和通信地址的美国住宅电话号码。从一个给定的电话号码,XML Web 服务可以返回通信地址、城市、州、邮政编码、姓名、提供商以及交换位置。DOTS YellowPages XML Web 服务开启了包含超过 1400 万个美国企业列表的企业名录,并且可以通过关键字、类别、名称或常用类别进行搜索。当将这些 Web 服务用作商业目的时,需要从 ServiceObjects 获得一个许可证。只有用于测试时,许可证密钥“0”才有效。

 该方案就是 Pocket PC 用户正在旅行中。People Anyplace 提供了准确的地址信息(人员的电话号码、联系人、朋友和业务等等),同时还提供了附近旅馆和餐馆的信息。

 People Anyplace

 此示例应用程序由两个窗体组成。主窗体允许用户输入电话号码。该电话号码被传递到 DOTS GeoPhone XML Web 服务,而该服务会返回与电话号码相关联的地址(包括邮政编码)。

 当用户点击“Find Hotels & Restaurants”后,会显示一个新的窗体,允许用户根据刚刚查找到的地址的邮政编码,在给定范围内搜索餐馆和旅馆。

 响应会填充树视图控件以增加可用性。现在,让我们看看这段代码。

 代码演练

 第一个 Web 服务被称为 GeoPhone。以下就是具体方法:

 private void btnFindPerson_Click(object sender,

 System.EventArgs e)

 {

 // Instantiate Web Service

 GeoPhone geoPhone = new GeoPhone();

 // Reset all controls to default

 resetControls();

 // Show wait cursor

 Cursor.Current = Cursors.WaitCursor;

 try

 {

 // Check that server is available over HTTP

 (port 80)

 if(CheckConnection.Once(geoPhone.Url))

 {

 // Get address

 Contact Person =

 geoPhone.GetPhoneInfo(txtPhoneNumber.Text,"0").Contacts[0];

 // Display address

 txtName.Text = Person.Name;

 txtAddress.Text = Person.Address;

 txtCity.Text = Person.City;

 txtState.Text = Person.State;

 txtZip.Text = Person.Zip;

 this.btnYellowPages.Enabled = true;

 }

 else

 {

 // No connection!

 MessageBox.Show("Connection to Web Service

 server could not be established!");

 }

 }

 catch (Exception ex)

 {

 // Disable Save As Contact button

 this.btnYellowPages.Enabled = false;

 MessageBox.Show("Could not make Web Service calls

 (" + ex.Message + ")!");

 }

 finally

 {

 // Restore default cursor

 Cursor.Current = Cursors.Default;

 }

 }

 在调用 Web 服务之前,首先验证服务器是否可用非常重要。在用户等待响应时,光标设置为沙漏。

 在返回响应后,用户可以点击“Find Hotel & Restaurants”来查找附近的旅馆和餐馆。使用以下代码来调用 YellowPages Web 服务:

 private void btnFindBusiness_Click(object sender,

 System.EventArgs e)

 {

 DOTSYellowPages YP = new DOTSYellowPages();

 Listings HotelListings = new Listings();

 Listings RestaurantListings = new Listings();

 // Show wait cursor

 Cursor.Current = Cursors.WaitCursor;

 try

 {

 // Check that server is available over HTTP (port

 80)

 if(CheckConnection.Once(YP.Url))

 {

 // Get hotels

 HotelListings = YP.GetYPListingsByCategoryID(1279, YPZip, int.Parse(txtMiles.Text), "0");

 // Get restaurants

 RestaurantListings = YP.GetYPListingsByCategoryID(655, YPZip, int.Parse(txtMiles.Text), "0");

 // Clear treeview

 tvwBusiness.Nodes.Clear();

 // Call to fill the treeview with hotels

 FillTreeView(HotelListings);

 // Call to fill the treeview with

 restaurants

 FillTreeView(RestaurantListings);

 }

 else

 {

 // No connection!

 MessageBox.Show("Connection to Web Service

 server could not be established!");

 }

 }

 catch (Exception ex)

 {

 MessageBox.Show("Could not make Web Service calls

 (" + ex.Message + ")!");

 }

 finally

 {

 // Restore default cursor

 Cursor.Current = Cursors.Default;

 }

 }

 可以在以下网址找到 YellowPages WSDL:http://ws2.serviceobjects.net/yp/YellowPages.asmx?WSDL

 让我们看一下 Web 服务响应中的内容:

 - <s:complexType name="Listing">

 - <s:sequence>

 <s:element minOccurs="0" maxOccurs="1" name="Category" type="s:string"

 />

 <s:element minOccurs="0" maxOccurs="1" name="Description"

 type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="CompanyName"

 type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="Address" type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="City" type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="State" type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="Zip" type="s:string" />

 <s:element minOccurs="0" maxOccurs="1" name="Phone" type="s:string" />

 <s:element minOccurs="1" maxOccurs="1" name="Latitude" type="s:decimal"

 />

 <s:element minOccurs="1" maxOccurs="1" name="Longitude" type="s:decimal"

 />

 </s:sequence>

 </s:complexType>

 People Anyplace 应用程序只使用几个返回的元素。请注意,同时还返回纬度和经度。这意味着我们可以添加一个非常酷的功能 - 打开原始地址附近的地图,旅馆/餐馆就会跃入眼帘了!将 GPS 集成到移动设备中的实现指日可待,我们将可以看到 Web 服务和移动性之间日趋完善的交叉行业的面貌。不是很酷吗?

 以下代码填充树视图控件:

 private void FillTreeView(Listings TreeListing)

 {

 TreeNode nod;

 try

 {

 tvwBusiness.BeginUpdate();

 foreach (Listing li in TreeListing.Listing)

 {

 nod = new TreeNode(li.CompanyName);

 nod.Nodes.Add(li.Address);

 nod.Nodes.Add(li.Phone);

 tvwBusiness.Nodes.Add(nod);

 }

 tvwBusiness.EndUpdate();

 }

 catch (Exception ex)

 {

 MessageBox.Show("Could not parse results (" +

 ex.Message + ")!");

 }

 finally

 {

 tvwBusiness.Nodes.Clear();

 }

 }

 小结

 有很多非常酷的产品和标准正在朝同一个方向发展。新的开发工具、Internet 连接性、移动设备和 Web 服务共同促进了全新软件和服务类别的产生。真正的移动 Internet 终将会到来!

查看原文地址

0
相关文章