Windows Phone开发(47):轻松调用Web Service

原文:Windows Phone开发(47):轻松调用Web Service

众所周知(除了没用过VS的),在VS里面调用Web Service是一件很愉快的事情,不解释,相信很多朋友在以前的项目中肯定也用过WEB服务。同样,在WP中调用Web Service也是非常简单的,你可以不信,反正我绝对信了。

 

有例子有真相,我们就以http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx

为例,这个Web服务可以根据IP地址查询相关的地区/城市信息,我们就通过调用这WEB服务,来比较一下,在WP项目调用Web Service与我们以前做过的其它类型到底有没有区别。

 

1、启动VS,新建一个WP应用程序项目(此处省略46个字)。

2、页面布局就参考下面XAML吧。

<phone:PhoneApplicationPage
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" VerticalAlignment="Center" Text="IP地址:"/>
                <TextBox Name="txtIP" Grid.Column="1"/>
                <Button Grid.Column="2" Click="onQuery">
                    <Button.Content>
                        <Path Data="M0,10 L20,10 M5,0 L20,10 M5,20 L20,10"
                              VerticalAlignment="Stretch"
                              HorizontalAlignment="Stretch"
                              Stroke="White" StrokeThickness="3"/>
                    </Button.Content>
                </Button>
            </Grid>
            <StackPanel Grid.Row="1">
                <TextBlock Name="txbTip"/>
                <TextBlock Name="txbResult" Margin="2,12,2,0" FontSize="32"/>
            </StackPanel>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

3、打开“解决方案资源管理器”,右击“引用”节点,从弹出的菜单中选择“添加服务引用”。

4、在弹出的对话框中,“地址”处输入上文中提到的Web服务的地址,并点击“前往”按钮,待发现WEB服务完成后,在“命名空间”处输入一个有效命名空间名字,随你喜欢。最后别忘了单击“确定”。

 

5、切换到后台代码,完成查询按钮的单击事件处理。

        private void onQuery(object sender, RoutedEventArgs e)
        {
            // 第一步,实例化客户端代理类
            IPQueryWebService.IpAddressSearchWebServiceSoapClient MyClient = new IPQueryWebService.IpAddressSearchWebServiceSoapClient();
            // 第二步,绑定回调事件
            MyClient.getCountryCityByIpCompleted += (s, arg) =>
                {
                    // 取得结果
                    txbTip.Text = "请求完成。";
                    if (arg.Error != null)
                    {
                        txtIP.Text = string.Format("错误:{0}", arg.Error.Message);
                        return;
                    }
                    string[] res = arg.Result;
                    if (res != null)
                    {
                        if (res.Length > 1)
                        {
                            txbResult.Text = string.Format("查询结果:{0}", res[1]);
                        }
                    }
                };
            // 第三步,调用异步方法
            txbTip.Text = "正在请求,请等候……";
            MyClient.getCountryCityByIpAsync(txtIP.Text);
        }

 

好了,完成,记住WEB服务一般是异步调用,所以要在回调事件处理中取得调用结果。

运行一下,看看结果吧。

 

OK,就到这里吧。

 

时间: 2024-08-31 16:15:58

Windows Phone开发(47):轻松调用Web Service的相关文章

wp8开发-WP8开发,调用web service后保存页面xml格式信息到本地.xml文件

问题描述 WP8开发,调用web service后保存页面xml格式信息到本地.xml文件 写了一个web service,这是调用后得到的结果,我想把这些xml格式的字符保存到手机生成一个XXX.xml的文件然后再解析,请问把那些字符保存并生成文件的代码怎么写呢

在ASP.NET Atlas中调用Web Service—处理错误

asp.net|web|错误 在本系列的上一篇(在ASP.NET Atlas中调用Web Service--介绍及简单应用)中,我们熟悉了Atlas中调用Web Service的最基础方法,但是在实际开发中,仅仅发出请求并等待返回结果是不够的,我们大都需要考虑对错误超时等的处理,也要允许用户取消操作.幸运的是,Atlas对Web Service中的Web Method的封装也充分考虑到了这些需求.让我们举一个Web Method的例子来说明,例如,对于如下的Web Method: public

在WebSphere Portlet Factory中调用Web Service

通过 WebSphere Portlet Factory(WPF),开发者非常方便实现 Web Service 和 Portlets 的通信.开发者无需花费大量的时间去学习 SOAP,WSDL,XML schema(xsd) 等 Web Service 技术,使用 WPF 即可开发出健壮.功能强大的 Web Service 和实现对各种风格的 Web Service 的调用.WPF 隐藏了 Web Service 的内部实现细节,通过 WPF 的丰富的构建器自动生成 Web Service 和实

使用Integrated Web Services Client for ILE在ILE程序中调用Web Service 一

IBM Integrated Web Services for i是IBM最新发布的在IBM i上的SOA解决方案,包括Integrated Web Services Server for i和Integrated Web Services Client for ILE两部分.Integrated Web Services Server for i使用户能够将ILE RPG 和 COBOL程序快速部署成Web Services,而Integrated Web Services Client fo

android调用web service(cxf)实例应用详解_Android

Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包 1.下载该夹包可以直接登录http://code.google.com/p/ksoap2-android/,现在该站点已经提供了直接的下载,只要点击下载链接就可以下载了: 我现在的是ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 2.好了,现在我们就可以进行新建项目来进行测试了,首先我们先建立java服务端,这里的一些前期

用cxf公布和调用web service

用cxf发布和调用web service 最近我们的系统需要和一个第三方系统对接,对接的方式是通过web service,所以就学习了一下这方面的东西 用CXF来做web service是比较简单的,本文就简单介绍一下如何一步步发布web service,以及调用现有的web service.另外如果系统已经使用了Spring MVC,那么引入CXF需要额外的步骤,见本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920.如果展现层没有用spring mvc,而

WinForm如何调用Web Service

参考地址 今天看了李天平关于WinForm调用Web Service的代码,我自己模仿做一个代码基本都是复制粘贴的,结果不好使.郁闷的是,又碰到那个该死的GET调用Web Service,我想肯定又是Web.config需要配置,结果WinForm没有这个配置文件,奇怪,为什么人家的就好使,我写的就不好使呢. 上网搜吧,唉,找个两个多小时,基本都是和我一样的代码,互相转载.根本没人提代码好不好使,也没人提正确的用法.就在我要放弃的时候,终于发现原来是在 Web Service的Web.confi

android调用web service(cxf)实例应用详解

Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包 1.下载该夹包可以直接登录http://code.google.com/p/ksoap2-android/,现在该站点已经提供了直接的下载,只要点击下载链接就可以下载了: 我现在的是ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 2.好了,现在我们就可以进行新建项目来进行测试了,首先我们先建立java服务端,这里的一些前期

手把手教你实现、部署和调用Web Service

手把手教你实现.部署和调用Web Service