当前位置:编程学习 > wap >>

Windows Phone 7 开发 31 日谈——第25日:外部API

 

第25日。

昨天我写了如何在你的应用程序中嵌入字体,视频和图片。今天,我们来讨论从Web Service中获取数据,并将它们填入到你的应用程序中。

介绍Twitter API

    如果你之前没有玩儿过这个,那你肯定会常听我将一个Twitter应用程序比喻为“Hello, world!”程序。原因很简单,因为几乎每一个应用程序都需要连接Web Service,Twitter的API用起来非常简单,并且是免费的,不需要任何注册。换句话说,你可以无障碍地介入,这是学习新技术的一种好方法。

    有关Twitter API的关键内容可以在这里找到:http://dev.twitter.com/。我们来看看用户时间线元素的API,利用这个指定的URL模板:http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=jeffblankenburg。这里的用户名,jeffblankenburg ,可以用任意的Twitter用户名替换。如果点击我给你提供的URL,你会看到很多XML文档。这个文档包含了我最近的Twitter信息,以及大量的元数据。以下是这个文档中的一个节点:

代码

<status>

  <created_at>Sun Oct 24 13:30:04 +0000 2010</created_at>

  <id>28594986565</id>

  <text>Day #24: Embedding Fonts in Windows Phone 7 http://bit.ly/wp7day24 #wp7 #wp7dev #31daysofwp7</text>

  <source>

    <a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>

  </source>

  <truncated>false</truncated>

  <favorited>false</favorited>

  <in_reply_to_status_id />

  <in_reply_to_user_id />

  <in_reply_to_screen_name />

  <retweet_count />

  <retweeted>false</retweeted>

  <user>

    <id>5688882</id>

    <name>Jeff Blankenburg</name>

    <screen_name>jeffblankenburg</screen_name>

    <location>Columbus, OH</location>

    <description>I'm a passionate technologist, husband, and father in Columbus, OH. I work for a small software company located in Redmond, WA. #wp7 http://blankensoft.com</description>

    <profile_image_url>http://a3.twimg.com/profile_images/396764567/jeffblankenburgheadshot_normal.jpg</profile_image_url>

    <url>http://www.jeffblankenburg.com</url>

    <protected>false</protected>

    <followers_count>1962</followers_count>

    <profile_background_color>131516</profile_background_color>

    <profile_text_color>333333</profile_text_color>

    <profile_link_color>994700</profile_link_color>

    <profile_sidebar_fill_color>cccccc</profile_sidebar_fill_color>

    <profile_sidebar_border_color>999999</profile_sidebar_border_color>

    <friends_count>652</friends_count>

    <created_at>Tue May 01 15:54:53 +0000 2007</created_at>

    <favourites_count>201</favourites_count>

    <utc_offset>-18000</utc_offset>

    <time_zone>Eastern Time (US & Canada)</time_zone>

    <profile_background_image_url>http://s.twimg.com/a/1287010001/images/themes/theme14/bg.gif</profile_background_image_url>

    <profile_background_tile>true</profile_background_tile>

    <profile_use_background_image>true</profile_use_background_image>

    <notifications>false</notifications>

    <geo_enabled>true</geo_enabled>

    <verified>false</verified>

    <following>true</following>

    <statuses_count>5664</statuses_count>

    <lang>en</lang>

    <contributors_enabled>false</contributors_enabled>

    <follow_request_sent>false</follow_request_sent>

    <listed_count>151</listed_count>

    <show_all_inline_media>false</show_all_inline_media>

  </user>

  <geo />

  <coordinates />

  <place />

  <contributors />

</status>

www.zzzyk.com

对于上面的内容要记住的是这仅仅是XML,没有什么神秘的,也没神秘特别的。网络中大多数Web Service都提供了XML源,一般来说,我们可以以一种统一的方式来处理它们。

将XML数据从网络中获取到我们的应用程序

    从手机中正在运行的应用程序上在线获取XML数据非常简单(只需3行!)。最重要的是要由你来检测用户是否已经获取到了连接。下面是一种非常简单的方法,使用Microsoft.Phone.Net.NetworkInformation程序集:

if (NetworkInterface.GetIsNetworkAvailable())

在这个循环中,我们要创建一个WebClient对象,并异步调用我刚刚给你的地址中的Twitter API。首先,我为数据检索完成后创建了一个事件处理程序,然后发起异步调用。(在这个例子中,你会看到我用了一个文本框中来获取用户输入的用户名)

代码

if (NetworkInterface.GetIsNetworkAvailable())

{

    WebClient twitter = new WebClient();

 

    twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);

    twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + TwitterNameBox.Text));

}

www.zzzyk.com

当返回数据时,事件处理程序会被调用,此时我们需要添加一些内容。

在应用程序中使用XML

    当我们程序中获取到了数据时,就要真正地在屏幕上显示它们了。在我的其他例子中,我展示了如何将一个值绑定到XAML控件上。但本文重点不在此(虽然下面的示例代码中包含这些内容),相反,我们来看看如何用LINQ来解析XML数据。

为此,需要引入另一个名称空间,System.Xml.Linq。把它加入后,数据就变的非常简单了。我们需要一个新的XElement对象来保存XML数据。

XElement xmlTweets = XElement.Parse(e.Result);

一旦xmlTweets中保存了我们的数据,剩下要做的就是将它绑定到一个ListBox中,并使用LINQ从这些数据中创建自定义的TwitterItem对象。

TwitterList.ItemsSource = from tweet in xmlTweets.Descendants("status") select new TwitterItem{message = tweet.Element("text").Value};

你会看到在下面的示例代码中,我自定义的TwitterItem类中包含一个“message”属性。

就是这样!我们从XML源中实时抓取数据,然后再程序中处理它们,最后将它们显示在ListBox中。至于完整的示例,参见下面的代码

 

下载示例代码

这是一个完全可以运行的(但肯定不是包含所有功能的)Twitter客户端。你可以在文本框中输入一个用户名,然后程序就会连接Twitter API,抓取数据,解析并在程序中显示。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,