技术开发 频道

Windows Phone7如何使用REST服务

  团购网站分站列表API的读取和解析

  下面我们就开始使用WebClient类来实现一个调用REST服务的实例。在该示例中主要是调用一个团购网站开发的REST API,以实现该团购网站在全国各地分部的列表。下面是该团购网站开放的API接口地址:http://www.meituan.com/api/v1/divisions

  打开这个链接我们看到如下的XML文档:

Windows Phone7如何使用REST服务

  我们现在要做的就是将这些分站的列表在WP7

  应用中显示出来。实际界面如下所示:

Windows Phone7如何使用REST服务

  下面是实现WP7上读取团购网站的完整代码,分为XAML和C#两部分:

  XAML:

<phone:PhoneApplicationPage
    x:Class
="MeiTuan.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 is the root grid where all page content is placed-->
    
<Grid x:Name="LayoutRoot" Background="Transparent">
        
<Grid.Resources>
            
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem" >
                
<Setter Property="Foreground" Value="Black"/>
                
<Setter Property="Background" Value="LightGray"/>
            
</Style>
            
<Style x:Key="ComboBoxStyle" TargetType="ComboBox" >
                
<Setter Property="Foreground" Value="Black"/>
                
<Setter Property="Background" Value="Gray"/>
            
</Style>
        
</Grid.Resources>
        
<Grid.RowDefinitions>
            
<RowDefinition Height="Auto"/>
            
<RowDefinition Height="*"/>
        
</Grid.RowDefinitions>
        
<!--TitlePanel contains the name of the application and page title-->
        
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            
<TextBlock x:Name="ApplicationTitle" Text="REST 示例"
Style
="{StaticResource PhoneTextNormalStyle}"/>
            
<TextBlock x:Name="PageTitle" Text="团购分站"
Margin
="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"
Height
="99" Width="453" />
        
</StackPanel>
        
<!--ContentPanel - place additional content here-->
        
<Grid x:Name="ContentPanel" Margin="12,139,12,0" Grid.RowSpan="2">
            
<Button Content="点击获取分站列表!" Height="80" HorizontalAlignment="Left"
Margin
="6,90,0,0" Name="button1"
VerticalAlignment
="Top" Width="275" Click="button1_Click" />
            
<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,1,0,0"
Name
="textBlock3"
Text
="REST API地址:" VerticalAlignment="Top" />
            
<TextBlock Height="56" HorizontalAlignment="Left" Margin="6,28,0,0"
Name
="uriTextBlock" TextWrapping="Wrap" Text="{Binding}"
VerticalAlignment
="Top" Width="447" />
            
<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,242,0,0"
Name
="textBlock5"
Text
="分站列表:" VerticalAlignment="Top" />
            
<ListBox Height="352" HorizontalAlignment="Left" Margin="6,271,0,0" Name="listBox1"
VerticalAlignment
="Top" Width="444" ItemsSource="{Binding}" >
                
<ListBox.ItemTemplate>
                    
<DataTemplate>
                        
<Border BorderBrush="{StaticResource PhoneForegroundBrush}" Width="418" BorderThickness="2"
Margin
="2">
                            
<StackPanel>
                                
<TextBlock Text="{Binding Path=CityName}" TextWrapping="Wrap" />
                            
</StackPanel>
                        
</Border>
                    
</DataTemplate>
                
</ListBox.ItemTemplate>
            
</ListBox>
        
</Grid>
    
</Grid>

</phone:PhoneApplicationPage>


C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace MeiTuan
{
    
public partial class MainPage : PhoneApplicationPage
    {
        
// REST API 地址
        
String requestString =
            
"http://www.meituan.com/api/v1/divisions";
      
        
public MainPage()
        {
            InitializeComponent();
          
            
// 创建 WebClient 实例,并和OpenReadCompleted事件进行关联.
            wc
= new WebClient();
            wc.OpenReadCompleted
+= new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
        }
        
// 调用团购网站分站列表服务
        WebClient wc;
        
private void CallToWebService()
        {
            
// 调用 OpenReadAsyc 生成一个 get 请求, 并将请求的url显示在文本框.
            wc.OpenReadAsync(
new Uri(requestString));
            uriTextBlock.DataContext
= string.Format(requestString);
        }

        
/// <summary>
        
/// 异步读取数据完成
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        void wc_OpenReadCompleted(
object sender, OpenReadCompletedEventArgs e)
        {
            XElement resultXml;
            
//查看是否有错误发生
            
if (e.Error != null)
            {
                return;
            }
            
else
            {
                try
                {
                    resultXml
= XElement.Load(e.Result);
                    
                    
//根据返回的XML文档生成分站信息对象.
                    var searchResults
=
                    from result in resultXml.Descendants(
"division")
                    
select new SearchResult
                    {
                        
// 只显示分站名
                        CityName
= result.Element("name").Value
                    };
                    
// 将列表框的数据源设置为获取到的分站信息数据
                    listBox1.DataContext
= searchResults;
                }
                catch (System.Xml.XmlException ex)
                {
                    uriTextBlock.Text
= ex.Message;
                }
            }
        }
        
private void button1_Click(object sender, RoutedEventArgs e)
        {
            CallToWebService();
        }
  
    }

    
public class SearchResult
    {
        
public string CityName { get; set; }
    }

}

 

  我们知道REST API大多数以XML或者Json之类的格式返回数据的,因此如何对这些XML和JSON数据进行解析就显得至关重要。本节的例子是使用LINQ来解析XML数据。这种语法相对于以前的XML文档解析可谓是非常简单和便捷,但是笔者并没有对该LINQ进行详细探讨,关于XML和JSON数据的解析将在下一节重点讲解。敬请期待。

0
相关文章