Pocket PC 调用动态 Web 服务






2.06/5 (8投票s)
一篇关于 Pocket PC 连接到动态 Web 服务的文章。
目录
引言
我一开始对 Pocket PC 项目一无所知。但是 Code Project 中的许多文章和其他我下面给出的 URL 给了我一个快速的开始。但是动态 URL 是最大的痛点。所以我希望与大家分享我的经验。
软件要求
Pocket PC 的软件要求是
- Microsoft ActiveSync
这是连接工作站和 Pocket PC 的工具,是与 Pocket PC 一起使用的第一个要安装的工具。
- MDAC 2.7
- Microsoft Compact Framework。
此框架将用于放置 Pocket PC 支持 .NET 应用程序所需的所有库。
硬件注意事项
- Pocket PC 需要一个串口来连接到工作站。
示例代码分析
我在这里完成的应用程序将创建一个 XML 字符串,并将该 XML 字符串传输到 Web 服务,Web 服务负责打开 XML 字符串并在下一个屏幕中显示它。此应用程序将从提供的文本框中获取字符串,并将其组合成 XML 格式的数据。这些 XML 数据从 Web 服务端接收并在文本框中显示。
//
// code for the data tansfer
//
private void btnTransfer_Click(object sender, System.EventArgs e)
{
try
{
StringBuilder sText = new StringBuilder();
sText.Append("<DATA>");
if(txtstr1.Text != "")
{
sText.Append("<string1>");
sText.Append(txtstr1.Text);
sText.Append("</string1>");
}
else if(txtstr2.Text != "")
{
sText.Append("<string2>");
sText.Append(txtstr2.Text);
sText.Append("</string2>");
}
sText.Append("</DATA>");
Homer.dataTransfer xmlTransfer = new Homer.dataTransfer();
xmlTransfer.Url = GetWebServiceUrl() ;
string strResponse = xmlTransfer.DataTransfer(sText.ToString());
MessageBox.Show("the message was " + strResponse);
}
catch(Exception exc)
{
lblResponse.Text = exc.Message;
}
}
//code for receving message from web service
以下是调用动态 URL 的代码。动态调用 Web 服务的方法是创建一个 INI 文件,例如,我的示例中的 webserviceUrl.ini。该文件的内容如下。该文件应放置在 Pocket PC 中的应用程序文件夹中。此应用程序无法通过模拟器进行尝试。您始终可以使用模拟器中的静态 Web 服务 URL。
//content of the webservice ini file
<?xml version="1.0" encoding="utf-8" ?>
<appConfig>
<webServiceUrl>
http://your_server_name_goes_here/SampleService/Service1.asmx
</webServiceUrl>
</appConfig>
Web 服务 INI 文件从下面的函数打开,Web 引用从 INI 文件中获取。
动态 XML Web 服务
在移动应用程序中使用 Web 服务更简单,但使其动态化是一项任务。由于 Pocket PC 必须在各种场景中进行测试,然后修改为实时服务器,因此动态 Web 服务对于移动应用程序而言与 Web 应用程序不同。以下代码将帮助您为应用程序设置动态 Web 服务 URL。
//
// Code for calling the dynamic URL
//
private string GetWebServiceUrl()
{
string Apppath = null;
Apppath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
string iniFile = Apppath + @"\WebServiceUrl.ini";
FileStream xmlStream = new FileStream(iniFile, FileMode.Open);
XmlTextReader xmlReader = new XmlTextReader(xmlStream);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
xmlReader = null;
xmlStream.Close();
XmlNode xmlRoot = xmlDoc.DocumentElement;
string Url = xmlRoot.ChildNodes.Item(0).ChildNodes.Item(0).Value;
xmlDoc = null;
return Url;
}
兴趣点
以上内容应能帮助任何人顺利开始使用 Pocket PC 应用程序。关于我在使用 Pocket PC 时遇到的一些注意事项和问题。
- 当我们使用 Pocket PC 应用程序窗体时,即使我们关闭窗体,实例仍将运行。因此,为了消除该问题,我必须将最小化按钮设置为 false,因此,当我们关闭 Pocket PC 中的窗体时,它会删除应用程序的实例。
- 当开发人员由于错误消息(例如应用程序已在运行实例)而无法运行应用程序时,以下链接可以帮助您消除该问题。
关于 Pocket PC 的其他有用链接
- .NET Compact Framework - 此链接包含所有需要参考 Pocket PC 的人的链接。
- 开发和部署 Pocket PC 安装应用程序 - 这是 Code Project 中一篇出色的文章,可帮助部署 Pocket PC 应用程序。
- Pocket PC 货币转换器 - 这是一篇了解 Pocket PC 和 Web 服务概念的好文章。
历史记录
创建日期 - 2005 年 3 月 22 日。