Url shortening services are getting popular, especially now that microblogging networks are extensively used. Since the length of a microblogging post is usually limited, a shortened url comes handy for it saves precious characters.

A shortened url is a sort of “compressed” url. For example if you point your browser to: http://is.gd/2 you will be redirected to google.com, this way saving seven characters. How comes? Because there are services which can build a shortened url given the original one. Every time you enter a shortened url they redirect your browser to the original. Maybe the case of google.com is not representative, because you save “only” seven characters. Think of posting a google maps address like this:



http://maps.google.com/maps?f=q&hl=en&geocode=&q=manhattan&ie=UTF8&z=17&iwloc=addr



Its shortened version is: http://is.gd/3k7B


Can you count how many characters do you save? Many!

Since I recently implemented a very similar feature in Posty, I will now show how to use a simple url shortener service in Adobe Air.

The interface will be made of:

  • a text input, where we can type the original url, to be shortened
  • a button, to trigger the shorten service
  • a text area to show the result

We put these three widgets in a VBox as in the following snippet of code.

1
2
3
4
5
<mx:VBox>
  <mx:TextInput id="originalURL" />
  <mx:Button id="shorten" label="Shorten" click="shortenLink()"/>
  <mx:Text id="result"/>
</mx:VBox>

The service which we’ll use is http://is.gd/ which is very reliable and simple to interact with. The api documentation is reported here. As you can see it is very easy to use the api. All it is needed is the url, provided as a parameter named longurl. For example, to shorten google.com, we have to call the following url:


http://is.gd/api.php?longurl=www.google.com


When we click the button we trigger the shortenLink() function (line 3), which is defined as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private var loader:URLLoader = new URLLoader();
 
private function shortenLink():void {
    var request:URLRequest = new URLRequest("http://is.gd/api.php?longurl="+originalURL.text);
    loader.addEventListener(Event.COMPLETE, shortenSuccess, false, 0, true);
    loader.addEventListener(IOErrorEvent.IO_ERROR, shortenError, false, 0, true);
    loader.load(request);
}
 
private function shortenSuccess(e:Event):void {
    loader.removeEventListener(Event.COMPLETE, shortenSuccess);
    loader.removeEventListener(IOErrorEvent.IO_ERROR, shortenError);
    result.text = originalURL.text + " shortened is " + e.currentTarget.data;
}
 
private function shortenError(e:IOErrorEvent):void {
    loader.removeEventListener(Event.COMPLETE, shortenSuccess);
    loader.removeEventListener(IOErrorEvent.IO_ERROR, shortenError);
    result.text = "Error in shortening " +originalURL.text;
}

As you can see in the code, we prepare a request to load the url (line 4) specified in the API documentation and we append the url inserted by the user.
The url shortener is a web service. You call it, but you do not get an immediate response. It takes a while, few seconds or milliseconds. This is why you need to set listeners, which are triggered when the service responds. We set up two listeners, one in case of success (line 5) and one triggered when there is some error in the process (line 6). In case of success, the listener function shows the shortened link (line 13). It notifies failure in case of error (line 19).

The only thing left is to couple logic and graphics by importing the actionscript file. Now our mxml file looks like this:

1
2
3
4
5
6
<mx:Script source="ShortenerController.as"/>
<mx:VBox>
  <mx:TextInput id="originalURL" />
  <mx:Button id="shorten" label="Shorten" click="shortenLink()"/>
  <mx:Text id="result"/>
</mx:VBox>

And we can run the code to get the response from the web service.


The source code for this post is available here.



ps:
In principle the same code works for Flex, but if you try to run it you’ll get a Sandbox violation message, because the Flash player is not allowed to load data from a different domain, unless the server specifies a cross-domain policy file.

Similar Posts

Share

Comments

  1. I knew of tinyurl.com, “is.gd/” saves some precious characters.

    the funny thing is i can “2″ about stuff, rather than google it.

    October 23, 2008
    - noj
  2. I kinda like my url’s long….and descriptive….and full of key words. But I try to think like a search engine and not like a developer or blogger.

    Daniel Simmons

    February 22, 2009
  3. The ideal to me is a shortened url with long url displayed as a tooltip. I know there are services which provide this feature, but don’t remember their name.

    February 23, 2009
  4. Leave a Reply