Checking HTTP Connection With AIR (Flash-Flex)

June 29, 2008 – 5:51 am

We have learned Adobe AIR as a platform which allows us to move internet application to desktop. Ok, what if there is no internet connection? Off course this is a low possibility but having internet connection is not mean is working with out problem. The AIR application you build it might need to download some files from internet, or as like web application may show some data. In that moment your application may want to figure the user computer has internet connection or not, this will help to figure out problems and show it to user.

We can reach that information by using the URLMonitor class. The basic idea behind that is, URLMonitor object is checking the port 80 with URLRequest so can figure out it is possible to make it HTTP request. If is possible the value return backs is true and if is not value will be false which means no internet connection available.

First we will see how we can check this in Flash CS3;

1-Open new AIR document
2-Get “AIR Service Monitor” from Components Panel and add to library.
3-Add a new dynamic text box to screen and write it “test_txt” as an instance name.
4-Add the code below in first key frame and test it.

  1. import air.net.URLMonitor;
  2. import flash.net.URLRequest;
  3. import flash.events.StatusEvent;
  4.  
  5. var monitor:URLMonitor = new URLMonitor(new URLRequest(‘http://www.enginyoyen.com’));
  6. monitor.addEventListener(StatusEvent.STATUS, checkHTTP);
  7. monitor.start();
  8.  
  9. function checkHTTP(e:StatusEvent) {
  10. if (monitor.available) {
  11. test_txt.text ="Internet is available";
  12. } else {
  13. test_txt.text ="No internet connection available";
  14. }
  15. }

In those codes first of all we are importing classes that we need, afterwards we are creating new URLMonitor object. Because of we will make in HTTP request we are creating the URLRequest object, and than we are giving a URL address. Than we are adding an event listener for checking the connection, if there is a change in connection status event listener will fired again and we ill be noticed from the latest status.

You can use same technique and working methods for Flex Builder, the only difference is you don’t have to make the first step in Flex Builder.

Code for flex;

  1. <mx:windowedapplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationcomplete="run();">
  2. <mx:script>
  3. <!–[CDATA[
  4. import air.net.URLMonitor;
  5. import flash.net.URLRequest;
  6. import flash.events.StatusEvent;
  7.  
  8. public var monitor:URLMonitor = new URLMonitor(new URLRequest("http://www.enginyoyen.com"));
  9.  
  10. public function run():void{
  11. monitor.addEventListener(StatusEvent.STATUS, checkHTTP);
  12. monitor.start();
  13. }
  14. public function checkHTTP(e:StatusEvent):void {
  15. if (monitor.available) {
  16. test_txt.text ="Internet is available";
  17. } else {
  18. test_txt.text ="No internet connection available";
  19. }
  20. }
  21. ]]–>
  22. </mx:script>
  23. <mx:label x="187" y="168" text="Checking…" id="test_txt">
  24. </mx:label>
  25. </mx:windowedapplication>

Take Care
Engin!


  1. 2 Responses to “Checking HTTP Connection With AIR (Flash-Flex)”

  2. I tried to use this example inside Flex (), but I keep getting this error inside the checkHTTP function:

    1120: Access of undefined property monitor.

    By Wally Kolcz on Oct 17, 2008

  3. Hi Wally,
    I think you are getting the error because you are not using those 2 lines of code in function;

    monitor.addEventListener(StatusEvent.STATUS, checkHTTP);
    monitor.start();

    I added complete and working version of code for flex at above in article, you can check from there.

    By Engin Yöyen on Oct 17, 2008

Leave a Reply