In this recipe, let's build a Twitter client app using the Twitter API. This is similar to the previous RSS reader recipe. Here, instead of RSS format you will receive the custom XML format related to the Twitter username or screen name.
First study the Twitter API resource page at:
https://dev.twitter.com/docs/api
There are many categories of API available such as Timelines, Tweets, Search, and so on. We will be using statuses/user_timeline
for a given user in this recipe.
In the next steps, we will create a client application, which will have a textbox to search for a name and return the latest updates for that username.
Recipe2_TwiitterClient
. TwitterFeed.cs
file. Open the file and add the new class to store the Twitter feed information. We will have one string variable called Content:
public class TwitterFeed { public string Content { get; set; } }
MainPage.xaml
file and change the ApplicationTitle
and PageTitle:
<!--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="CH6-Recipes" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="Twitter Client" Margin="9,- 7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel>
Search TextBlock
and a TextBox
following it:<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="BillGates" VerticalAlignment="Top" Width="460" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="Search:" VerticalAlignment="Top" /> <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" /> </Grid>
ListBox
along with a TextBlock
inside the Grid
control "ContentPanel2":
<Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1"> <ListBox x:Name ="lstItems" Margin="0,6,0,0"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="20"/> </Grid.RowDefinitions> <StackPanel VerticalAlignment="Top"> <TextBlock Grid.Row="0" Text="{Binding Content}"/> <TextBlock Grid.Row="1" /> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
MainPage.xaml.cs
file and add the following code using the WebClient
class and then DownloadStringCompletedEventHandler:
public partial class MainPage : PhoneApplicationPage { // Constructor WebClient remoteXml; public MainPage() { InitializeComponent(); remoteXml = new WebClient(); remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler (remoteXml_DownloadStringCompleted); } private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) return; XDocument xdoc = XDocument.Parse(e.Result); List<TwitterFeed> tweets; tweets = (from entry in xdoc.Descendants("status") select new TwitterFeed() { Content = entry.Element("text").Value, }).ToList(); lstItems.ItemsSource = tweets; }
DownloadStringAsync
to navigate to the URL passed:private void button1_Click(object sender, RoutedEventArgs e) { string txtSearch = textBox1.Text; string txtUri = "http://api.twitter.com//1/statuses/ user_timeline.xml?screen_name=" + txtSearch; Uri uri = new Uri(txtUri, UriKind.Absolute); remoteXml.DownloadStringAsync(uri); }
In the preceding recipe, you got the standard RSS formatted XML, which is similar across all RSS feeds. Here twitter provides its own set of APIs to provide different XML content. One such API is user timeline, which takes the input screen_name
and returns all the tweet updates for that user.
We used the WebClient
class to navigate and download the results of the API call. In the DownloadStringCompleted
event method, we used LINQ to XML. Here we parsed the returned XML file into the XDocument
object and then queried the XML document for text
nodes.
In this recipe, you understood how simple it is to call any REST API, download the result, and save it in local objects. You can refer to the Twitter API reference document to build a robust client app.