Building a Simple Client
Now I'll create a simple client that displays statuses on-screen. This application has two parts: a simple WPF-based ListBox to format status objects, and a class that abstracts the complexities of Twitter's XML or JSON formats.
Listing 2 is a domain object representing a status update. It has three properties: the message text, the poster's screen name, and the URL to the poster's image.
Here's the XAML for a formatted ListBox capable of showing the status updates pulled from the URL you looked at in the beginning of this article.
<ListBox IsSynchronizedWithCurrentItem="True" x:Name="lstResults" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Image Height="100" Width="100"
Source="{Binding Path=ImageURL}"/>
<StackPanel>
<TextBlock Text="{Binding Path=Text}"/>
<TextBlock Text="{Binding Path=ScreenName}"/>
<TextBlock Text="----------"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Finally, the following code puts it all together to both query and display the Twitter data.
Dim TwitterLib As New TwitterLib
Dim URL As String = _
"http://.../statuses/user_timeline/rodpaddock.json"
Dim Results As String = TwitterLib.GetTweet(URL)
Dim JsonSerializer As New _
System.Web.Script.Serialization.JavaScriptSerializer
Dim ReturnObject As Object = _
JsonSerializer.DeserializeObject(Results)
Dim StatusList As New List(Of Status)
For Each StatusObject As Object In CType(ReturnObject, Array)
StatusList.Add(New Status With _
{ _
.Text = StatusObject("text"), _
.ScreenName = StatusObject("user")("screen_name"), _
.ImageURL = StatusObject("user")("profile_image_url") _
})
Next
Me.lstResults.ItemsSource = StatusList
The code requests the timeline for
rodpaddock in JSON format, and converts the returned data to a .NET array using the JSON serializer. That array is then transformed into a list of simple POCO (Plain Old CLR Objects) which the sample WPF form displays.
You can see the final results of this in
Figure 2.
 | |
Figure 2. Simple Twitter WPF Client: Here are the results of querying Twitter for the rodpaddock timeline. |
Twitter Complete
The Twitter API might look daunting at first, but with a simple set of tools and techniques you can Twitter-enable your applications in a matter of minutes. And if you are looking for something to follow on Twitter, check out the following:
- http://twitter.com/codemagazine
- http://twitter.com/codecast
For more information about programming Twitter, check out these resources: