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!

    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
  • Guest Poster
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
Browse > Home / Examples / Connect to Twitter from AIR example

Connect to Twitter from AIR example

10 November 2009

Line Break

Author: Arjan (36 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

Written by Arjan · Filed Under Examples 

Was this post useful to you?

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

Comments

5 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

Get Adobe Flash player

  • Sponsors

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • Flash Builder
    • Flash Builder 4
    • Flashvars
    • Flex 4
    • Framework
    • HSlider
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
    • VSlider
  • Flex Blog Readers

  • Flex Jobs (from Flex Jobs.org)

    Your Job here? Post your job @ Flex Jobs.org

    Advertisement

  • Recent Posts

    • SWIZ Framework in Flex 4 Example: Part I
    • Dynamic AdvancedDataGridColumn in Flex 4
    • How to make a magnetic button in Flex 4
    • Using ASDoc as an External Tool in Flash Builder 4
    • Flex Encryption (MD5, SHA1, SHA224, SHA256, HMAC)
    • The SWIZ framework for Flex 3 / Flex 4: Easy, Light and Very Powerfull
    • Call Javascript function from Flex / Flash Builder (AS3)
    • Flex FlashVars in AS3 Example
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction
    • WordPress Flash / Flex Comments Form
  • Categories

    • Examples
    • Guest Poster
  • Archives

    • 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

    • List Directory with AIR in Flex 4 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Control Webpage in HTML Control in AIR 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 5 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 55 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Progressbar in Datagrid 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)
    • WordPress Flash / Flex Comments Form 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)