Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / Connect to Twitter from AIR example

Connect to Twitter from AIR example

10 November 2009

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


So everybody’s using Twitter now, so i thought it might be cool to write a small example to show you how you can connect to Twitter from an AIR application. The first thing you need is an API to connect to Twitter.

I’m using TwitterScript in my example so if you want to follow along with the example I recommend you download the *.swc file from their website. And add it to your library path by going to your project properties:

Add *.swc file to Flex project

There are a couple more Twitter APIs available for ActionScript. I don’t have any experience with them yet, so I can’t tell you which one is the best at the moment.

Ok let’s make simple example that does the following:

  • Let’s the user search for a Twitter user
  • Get’s all the Tweets from that user and lists them in a DataGrid
  • Add a text field to view the complete Tweet once a Tweet is selected in the DataGrid
  • Add an image that show the Twitter users picture
  • Display the Twitter users name

Searching for a Twitter user and handling the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import mx.collections.ArrayCollection;
import mx.managers.CursorManager;
           
import twitter.api.data.TwitterUser;
import twitter.api.Twitter;
import twitter.api.events.TwitterEvent;
import twitter.api.TwitterSearch;
import twitter.api.data.TwitterStatus;
           
[Bindable]
private var twitterStatusCollection:ArrayCollection;
           
private function doSearch():void
{
    CursorManager.setBusyCursor();
    var twit:Twitter = new Twitter();
    twit.addEventListener( TwitterEvent.ON_USER_TIMELINE_RESULT, userTimelineResult);
    twit.loadUserTimeline(tiTwitterUser.text);
}
           
private function userTimelineResult(event:TwitterEvent):void
{
    twitterStatusCollection = new ArrayCollection();
    for each( var status:TwitterStatus in event.data )
    {
        twitterStatusCollection.addItem(status);
    }
    CursorManager.removeBusyCursor();
}

The layout that shows all the information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<mx:VBox width="100%"
    height="100%" paddingTop="50"
    backgroundColor="#FFFFFF"
    horizontalAlign="center">
           
    <mx:HBox width="250">
            <mx:Label text="Twitter user:" fontWeight="bold"/>
        <mx:TextInput id="tiTwitterUser" width="100%"/>
    </mx:HBox>
    <mx:HBox width="250" horizontalAlign="right">
        <mx:Button label="Get Tweets!" click="doSearch()"/>
    </mx:HBox>
       
    <mx:VBox width="250"
        horizontalAlign="center">
        <mx:Text text="Name: {twitterStatusCollection.length > 0 ? twitterStatusCollection.getItemAt(0).user.name : ''}"
            fontWeight="bold"/>
        <mx:Image source="{twitterStatusCollection.length > 0 ? twitterStatusCollection.getItemAt(0).user.profileImageUrl : ''}"
                width="150"
                maintainAspectRatio="true"/>
        <mx:DataGrid dataProvider="{twitterStatusCollection}"
                width="250"
                id="dg">
            <mx:columns>
                <mx:DataGridColumn headerText="Tweets" dataField="text"/>
            </mx:columns>
        </mx:DataGrid>
            <mx:TextArea width="250" text="{dg.selectedItem.text}" height="100"/>
    </mx:VBox>
       
</mx:VBox>

Unfortunately it’s still not simple to connect to Twitter from Flex applications directly (because of restrictions on Twitters side, crossdomain.xml stuff which doesn’t allow requests from Flash movies outside of the twitter domain). It is possible though, you have can some proxy on the server (for example in PHP) to connect to Twitter and send requests and receive responses via that proxy.

The complete AIR application, however looks something like this, download it here. Don’t have the AIR runtime yet? Download it here:

Final AIR Application Connected to Twitter

Related posts:

  1. Twitter Search with Flex Builder
  2. Connect to Google Analytics from Flash Builder
  3. List Directory with AIR in Flex 4
  4. Progressbar in Datagrid Example

Written by Arjan · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.33 out of 5)
Loading ... Loading ...

86cc6c1cc6edf95c2813a2dfbed5c9f2delicious

Comments

8 Responses to “Connect to Twitter from AIR example”

  1. mike on January 23rd, 2010 2:12 am

    Good Job man, the code is clean and works great! good to get started.

    Thanks for sharing!

    Mike

  2. Tweety on February 26th, 2010 9:29 am

    Great job dude, thanks

  3. miguel on March 17th, 2010 4:32 pm

    Hi, thanks for the code!, I´m working with Flex 3 and I have problems with the loadUserTimeline function. Flex dont´t recognize this. I´m wondering if you could know what´s the problem.

  4. Arjan Nieuwenhuizen on March 18th, 2010 12:05 pm

    Sounds strange, the example is also in Flex 3. Are all the import statements there and is the swc file added to your library path as explained? If it is, please give me a more detailed description of the error message Flex gives you.

  5. Tanzeem on April 28th, 2010 7:54 am

    Very Well Done
    I want to post a message on Twitter..Please help me for posting a message
    Regrads
    Tanzeem Akhtar Bhatti
    Flex Developer

  6. mohammad rafiq on November 23rd, 2010 12:41 pm

    hey am getting an error of implicit coercion of type String to int at this statement
    twit.loadUserTimeline(tiTwitterUser.text);

  7. Arjan on November 23rd, 2010 1:52 pm

    Sounds strange. Are you sure your code / mxml is correct? The tiTwitterUser is a TextInput field and the property text always returns a string. The loadUserTimeline takes a string as an argument so everything should be fine….I tested it quickly again and no problems…

  8. Akshay on July 14th, 2011 3:19 pm

    thanks

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · Adobe® and Adobe® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)