You have to be online before you can upload anything, that's a given, the same also applies for your Adobe AIR project. Now that I have images in Flickr Floater, the next thing that the application needs is to be able to determine if it's online so that calls to the Flickr API don't fail.
Having classes that respond to network events means that you can incorporate connectivity detection into your Adobe AIR application—you can build logic that'll determine whether it's online or not.
To begin, you will need to have an event listener added to
the Adobe AIR shell property to monitor the Event.NETWORK_CHANGE event. You use this to find out when the network status has changed. The event
listener is added through the init() function. At the same time, I'm also
calling the function checkNetworkConnection() to test for a
connection on application start-up.
Within the function, you make a call to a test URL on the
Flickr API that allows you to ping the server to check network connections. You
then add a series of event listeners to monitor the connection, checking to see
whether you can make an HTTP call and whether that results in an error. The
various functions fired by the listeners set the value of a bindable variable networked to true or false depending on the event that's firing. You check the value of
this variable throughout the application to react to the network connection
changing. The following code demonstrates network status detection:
import flash.desktop.NativeApplication;
[Bindable]
private var networked:Boolean = false;
private function init():void {
NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, checkNetworkConnection);
checkNetworkConnection();
}
private function checkNetworkConnection(event:Event=null):void{
var headRequest:URLRequest = new URLRequest();
headRequest.method = "HEAD";
headRequest.url =
"http://api.flickr.com/services/rest/?method=flickr.test.echo";
var connectTest:URLLoader = new URLLoader(headRequest);
connectTest.addEventListener(HTTPStatusEvent.HTTP_STATUS,
connectHttpStatusHandler);
connectTest.addEventListener(Event.COMPLETE,
connectCompleteHandler);
connectTest.addEventListener(IOErrorEvent.IO_ERROR,
connectErrorHandler);
}
private function connectHttpStatusHandler(event:*=null):void{
if (event.status == "0" ? networked = false:networked
= true);
}
private function connectErrorHandler(event:IOErrorEvent):void{
networked = false;
}
private function connectCompleteHandler(event:Event):void{
networked = true;
}