Searching Twitter on Windows Phone 7

February 24, 2011

In the previous post we’ve seen how to get list of trends on twitter.
On this post we continue to explore twitter service. We will see you how to search for twits on twitter.

image

Searching Twitter

Twitter exposes a JSON-based search service in the following address:

http://search.twitter.com/search.json?q={0}

where {0} should be replaced with your search term.

The results are returned in JSON data format, so we will use again the DataContractJsonSerializer. For more details on how to use it, check out the previous post.

Defining Twit and TwitterResults

Similarly to what we’ve done in the previous post, we need to declare some C# model classes that will be used to parse the twitter JSON results.

/// <summary>
/// Model for twit
/// </summary>
public class Twit
{
    /// <summary>
    /// Gets or sets the text.
    /// </summary>
    /// <value>The text.</value>
    public string text { get; set; }

    /// <summary>
    /// Gets the decoded text.
    /// </summary>
    /// <value>The decoded text.</value>
    public string DecodedText
    {
        get
        {
            return HttpUtility.HtmlDecode(text);
        }
    }

    /// <summary>
    /// Gets or sets the from_user.
    /// </summary>
    /// <value>The from_user.</value>
    public string from_user { get; set; }

    /// <summary>
    /// Gets or sets the profile_image_url.
    /// </summary>
    /// <value>The profile_image_url.</value>
    public string profile_image_url { get; set; }

    /// <summary>
    /// Gets or sets the created_at.
    /// </summary>
    /// <value>The created_at.</value>
    public DateTime created_at { get; set; }
}

/// <summary>
/// Model for twitter results
/// </summary>
public class TwitterResults
{
    /// <summary>
    /// Gets or sets the results.
    /// </summary>
    /// <value>The results.</value>
    public Twit[] results { get; set; }
}

Note that the twit text should be html-decoded before using it, so I’ve added a DecodedText property to do the job.

Implement the Twitter Search Service

We will implement a static method named TwitterService.Search that will receive the search term as its first parameter and a few delegates to allow our class to work asynchronously:

  • Action<IEnumerable<Twit>> onSearchCompleted, which will be called when the twitter search is complete.
  • Action<Exception> onError, which will be called if there is an error while searching twitter.
  • Action onFinally, which will be called always, whether there was an exception or not. Think of it as the finally section on a common try-catch block.

So the method signature will be:

public static void Search(string searchText, Action<IEnumerable<Twit>> onSearchCompleted = null, Action<Exception> onError = null, Action onFinally = null)

To search twitter we will use the WebClient class, yet again:

WebClient webClient = new WebClient();

// register on download complete event
webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
{
    …
};

string encodedSearchText = HttpUtility.UrlEncode(searchText);
webClient.OpenReadAsync(new Uri(string.Format(TwitterSearchQuery, encodedSearchText)));

where TwitterSearchQuery is defined as follows:

private const string TwitterSearchQuery = "http://search.twitter.com/search.json?q={0}";

The rest of the code handles the different delegates: on SearchCompleted, onError, onFinally. I bring here the method in its full:

/// <summary>
/// Searches the specified search text.
/// </summary>
/// <param name="searchText">The search text.</param>
/// <param name="onSearchCompleted">The on search completed.</param>
/// <param name="onError">The on error.</param>
public static void Search(string searchText, Action<IEnumerable<Twit>> onSearchCompleted = null, Action<Exception> onError = null, Action onFinally = null)
{
    WebClient webClient = new WebClient();

    // register on download complete event
    webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
            // report error
            if (e.Error != null)
            {
                if (onError != null)
                {
                    onError(e.Error);
                }
                return;
            }

            // convert json result to model
            Stream stream = e.Result;
            DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(TwitterResults));
            TwitterResults twitterResults = (TwitterResults)dataContractJsonSerializer.ReadObject(stream);

            // notify completed callback
            if (onSearchCompleted != null)
            {
                onSearchCompleted(twitterResults.results);
            }
        }
        finally
        {
            // notify finally callback
            if (onFinally != null)
            {
                onFinally();
            }
        }
    };

    string encodedSearchText = HttpUtility.UrlEncode(searchText);
    webClient.OpenReadAsync(new Uri(string.Format(TwitterSearchQuery, encodedSearchText)));
}

Using the Twitter Search Service

Using the service is easy:

TwitterService.Search(
    textbox.Text,
   (items) => { listbox.ItemsSource = items; },
   (exception) => { MessageBox.Show(exception.Message); },
   null
   );

There is a sample application which can be downloaded here.

Note: this code was first published as part of the “Using Pivot and Panorama Controls” lab found in the Windows Phone Training Kit for Developers, which I wrote for Microsoft.

That’s it for now,
Arik Poznanski.

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. Jimmy SaenzFebruary 26, 2011 ב 20:11

    Great post!
    Do you have a example of a Simple Silverlight Twitter Client for Web? without authentication (only using http://api.twitter.com/1/statuses/user_timeline.xml)