VISUAL STUDIO 2010 CONSUMING THE TWITTER API WITH WINDOWS PHONE LAB 01 COPYRIGHT 2011 MICROSFOT WINDOWS
Creating a mobile application (for consuming the Twitter api) Expected duration: 15 minutes Contacts: Pablo Ochoa, Andres Muoz, Richard Orellana, Xavier Lombeida Description of May 07, 2011 Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 Ultimate is the comprehensive suite of application lifecycle management tools for teams to ensure quality results, from design to deployment. Whether you're creating new solutions or enhancing existing applications, Visual Studio 2010 Ultimate lets you bring your vision to life targeting an increasing number of platforms and technologiesincluding cloud and parallel computing. Eliminate 'no repro' with IntelliTrace The new IntelliTrace feature makes the "no-repro" discussion a thing of the past. Testers can file rich and actionable bugs with system information, and even include an environment snapshot. That way developers can reproduce the reported bug in the state it was found Understand existing architecture The Architecture Explorer helps you understand and unleash the value of your existing code assets and their inter-dependencies. You can produce detailed models of exactly how an application is constructed and even drill-down into specific areas for a deeper understanding. Ensure architectural compliance Use the new layer diagram to define and communicate logical application architecture and to validate code artifacts against the required architecture. Layer diagrams help your development efforts stay on track so your application conforms to your original vision Embrace Manual Testing Visual Studio Test Professional 2010, part of Visual Studio 2010 Ultimate, provides you with a single tool to capture and update test requirements, automate navigation of manual tests, and
speed up the fix and accept cycle by capturing the entire test context. This provides developers with everything they need to ensure that every error is reproducible. Be proactive about agile project management Visual Studio 2010 Ultimate is optimized for today's iterative development process with features that help you remain productive and react to potential risks before they happen. You can monitor the health of your project using reports that are automatically generated. Plus manage your project's capacity with historical data and Microsoft Excel-based planning documents. Windows phone Windows Phone, formerly known as Windows Mobile is a compact mobile operating system developed by Microsoft, designed for use in smartphones (Smartphones) and other mobile devices. Phone is part of Windows operating systems with natural user interface. It is based on the core Windows CE operating system and has a set of core applications using the Microsoft Windows API. It is designed to be similar to desktop versions of Windows aesthetically. In addition, a wide range of third party software available for Windows Mobile, which is available through Windows Marketplace for Mobile. Originally appeared under the name Pocket PC, as an offshoot of the development of Windows CE for mobile devices with limited capabilities. Currently, most Windows Mobile phones come with a digital pen that is used to enter commands by pressing on the screen. Windows Mobile has evolved and changed its name several times during its development, the latest version of Windows called Phone 7, announced on February 15, 2010, subject to availability at the end of 2010. Prerequisites Knowledge of programming in .net System Requirements Supported OS: Windows 7, Windows XP, Windows Vista Memory requirement: 512 minimum Disk space requirement: 500M byte Software needed for the lab Install Microsoft Visual Studio 2010 Install Service Pack 1 for Microsoft Visual Studio 2010 Install Windows Phone Emulator
Introduction Twitter is a well known application within social networks, which allows users to perform activities (basic) as: Make tweets. View timelines Perform authentication. Search users
There are actually three APIs The Twitter API currently consists of two discrete REST APIs and a Streaming API. Most application developers mix and match the APIs to produce their application. The separation of the REST and Search APIs is less than ideal and it is entirely due to history It is in our pipeline to ameliorate Twitters API by combining the Search and REST pieces as development cycles allow. The API Overview portion of the Getting Started series explains the history. The Streaming API provides low-latency high-volume access to Tweets. There are limits to how many calls and changes you can make in a day API usage is rate limited with additional fair use limits to protect Twitter from abuse. The API is entirely HTTP-based Methods to retrieve data from the Twitter API require a GET request. Methods that submit, change, or destroy data require a POST. A DELETE request is also accepted for methods that destroy data. API Methods that require a particular HTTP method will return an error if you do not make your request with the correct one. HTTP Response Codes are meaningful The API is a RESTful resource With the exception of the Streaming API, the Twitter API attempts to conform to the design principles of Representational State Transfer (REST). Simply change the format extension a request to get results in the format of your choice. The documentation notes which formats are available for each method. The API presently supports the following data formats: XML, JSON, and the RSS and Atom syndication formats, with some methods only accepting a subset of these formats.
Parameters have certain expectations Some API methods take optional or requisite parameters. Two things to keep in mind when making requests with parameters:
Parameter values should be converted to UTF-8 and URL encoded. The page parameter begins at 1, not 0.
There are two special parameters in the Twitter API:
callback: Used only when requesting JSON formatted responses, this parameter wraps your response in a callback method of your choice. For example, appending callback=myFancyFunction to your request will result in a response body of: myFancyFunction(...). Callbacks may only contain alphanumeric characters and underscores; any invalid characters will be stripped. suppress_response_codes: If this parameter is present, all responses will be returned with a 200 OK status code - even errors. This parameter exists to accommodate Flash and JavaScript applications running in browsers that intercept all non-200 responses. If used, its then the job of the client to determine error states by parsing the response body. Use with caution, as those error messages may change.
Where noted, some API methods will return different results based on HTTP headers sent by the client. Where the same behavior can be controlled by both a parameter and an HTTP header, the parameter will take precedence. There are pagination limits Rest API Limit Clients may access a theoretical maximum of 3,200 statuses via the page and count parameters for the user_timeline REST API methods. Other timeline methods have a theoretical maximum of 800 statuses. Requests for more than the limit will result in a reply with a status code of 200 and an empty result in the format requested. Twitter still maintains a database of all the tweets sent by a user. However, to ensure performance of the site, this artificial limit is temporarily in place. Search API Limit Clients may request up to 1,500 statuses via the page and rpp parameters for the search method. The response to a request exceeding this limit will be a status code of 403 and the error message page parameter out of range in the format requested. This artificial limit is in place to ensure the performance of the search system. We also restrict the size of the search index by placing a date limit on the updates we allow you to search. This limit varies depending on the number of Tweets being created and the relevance of the results.
There are Twitter API libraries for almost any language The community has created numerous Twitter API libraries. If you know of others we havent got let us know. Architecture with Windows Phone
Windows Phone Tools to use
Application development cycle Windows Phone
Exercise:
Opening Visual Microsoft Studio 2010 Express for Windows Phone
Select a new application with C# lenguage
Select de Windows phone version
This is a interface for to work in Windows phone
Change the name in the Application
Dragging the elements in the window Text box List box Botton
And change de names
Add a new class for the conecction to Api Twitter
Select a new Class C#
Putt the code in the class, to describe of variabels that they show up
using using using using using using using using using using
System; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Ink; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes;
namespace TwitterExample { public class TwitterItem { public string UserName { get; set; } public string Message { get; set; } public string ImageSource { get; set; }
} }
Select a Main Page.xaml.cs
Insert the code in MainPage.xaml.cs
using using using using using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes; Microsoft.Phone.Controls; System.Xml.Linq;
namespace TwitterExample { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } private void btnLookup_Click(object sender, RoutedEventArgs e) { 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="+txtLookup.Tex t)); } void twitter_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e){ if (e.Error != null) return; XElement xmlTweets = XElement.Parse(e.Result); tweetList.ItemsSource = from tweet in xmlTweets.Descendants("status") select new TwitterItem { ImageSource = tweet.Element("user").Element("profile_image_url").Value, Message = tweet.Element("text").Value, UserName = tweet.Element("user").Element("screen_name").Value }; } } }
Change the name of button in application for to use in void function
Add a reference in the code System.Xml.Linq
In the code of XML interface, put the code in the list box
<ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Height="132"> <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"> </Image> <StackPanel Width="370"> <TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" FontSize="38"></TextBlock> <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24"></TextBlock> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate>
Run the application, press F5
Search in the textbox, users' names that they exist in twitter