Windows Phone 7 网络编程之调用web service

2012-2-16 16:11| 发布者: Vincent| 评论: 4|原作者: SilverLight

下面通过一个手机号码归属地查询例子来演示Windows Phone 7的应用程序如何调用web service 接口。
先看一下运行的效果:
2011042119205971.jpg

应用调用的手机号码归属地查询的web service接口为:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
第一步  添加webservice的引用,将web service服务加入,这时生成了上述web服务在本地的一个代理。
由于.net平台内建了对Web Service的支持,包括Web Service的构建和使用,所以在Windows Phone 7项目中你不需要其他的工具或者SDK就可以完成Web Service的开发了。
2011042119235632.jpg

添加web service引用后,项目的文件目录如下:
2011042119250084.jpg

多了MobileReference服务和ServiceReferences.ClientConfig文件
第二步  调用web service
先看一下XAML界面代码
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  2.             <TextBlock Height="49" HorizontalAlignment="Left" Margin="12,66,0,0" Name="des" Text="请输入你需要查询的手机号码" VerticalAlignment="Top" Width="284" />
  3.             <TextBox Height="72" HorizontalAlignment="Left" Margin="6,106,0,0" Name="No" Text="" VerticalAlignment="Top" Width="415" />
  4.             <Button Content="查询" Height="72" HorizontalAlignment="Left" Margin="12,184,0,0" Name="search" VerticalAlignment="Top" Width="160" Click="search_Click" />
  5.             <TextBlock Height="211" HorizontalAlignment="Left" Margin="6,277,0,0" Name="information" Text="" VerticalAlignment="Top" Width="444" />
  6.         </Grid>
复制代码
调用web service服务,代码很简洁。。。
  1. public partial class MainPage : PhoneApplicationPage
  2.     {
  3.         public MainPage()
  4.         {
  5.             InitializeComponent();
  6.         }

  7.         private void search_Click(object sender, RoutedEventArgs e)
  8.         {
  9.             //实例化一个web service代理的对象
  10.             MobileReference.MobileCodeWSSoapClient proxy = new MobileReference.MobileCodeWSSoapClient();
  11.             //getMobileCodeInfo方法调用结束之后 触发的事件
  12.             proxy.getMobileCodeInfoCompleted+=new EventHandler<MobileReference.getMobileCodeInfoCompletedEventArgs>(proxy_getMobileCodeInfoCompleted);
  13.             //将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端  
  14.             //这里对应的是调用了web service的getMobileCodeInfo方法
  15.             proxy.getMobileCodeInfoAsync(No.Text, "");
  16.         }

  17.         void proxy_getMobileCodeInfoCompleted(object sender, MobileReference.getMobileCodeInfoCompletedEventArgs e)
  18.         {
  19.             if (e.Error == null)
  20.             {
  21.                 //显示返回的结果
  22.                 information.Text = e.Result;
  23.             }
  24.         }
  25.     }
复制代码




XBear1986 2012-2-22 11:37
顶V哥,我还用WebClient去下载了,不知这两种方式,有什么区别啊?
Micro-Sanvey 2012-2-19 23:51
学习了
xiebaochun 2012-2-19 23:33
thinks
liuqiu 2012-2-19 07:52
学习,不错

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 17:14

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部