Detecting network connection for your AIR application

Geplaatst op 7 februari 2008 door Gerdt Brouwer

I was looking at the internet for the simple solution to see if my air app has connection with the internet.

I found a lot of blogs, all telling me a little more, but no direct good demo.
This is the source I use while I work with AIR beta 3.

CODE:
  1. [XML]
  2.  
  3. <mx:windowedapplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationcomplete="init()" title="Network Change Event Log">
  4. <mx:script>
  5. <!--[CDATA[
  6. import flash.events.NetStatusEvent;
  7. import flash.net.URLRequest;
  8.  
  9. public function init():void{
  10. NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE,onNetworkChange);
  11. }
  12.  
  13. public function onNetworkChange(networkChangeEvent:Event):void{
  14. var now:String = new Date().toTimeString();
  15. logEvent("Network change event at: " + now);
  16. ping();
  17. }
  18.  
  19. public function ping():void{
  20. var headRequest:URLRequest = new URLRequest();
  21. headRequest.method = "HEAD";
  22. headRequest.url = resourceURL.text;
  23.  
  24. logEvent("Requesting: " + headRequest.url.toString());
  25. var response:URLLoader = new URLLoader(headRequest);
  26. response.addEventListener(HTTPStatusEvent.HTTP_STATUS,onHTTPStatus);
  27. response.addEventListener(Event.COMPLETE,onComplete);
  28. response.addEventListener(IOErrorEvent.IO_ERROR,onError);
  29. response.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
  30. }
  31.  
  32. public function onComplete(status:Event):void{
  33. logEvent("    Response received.");
  34. }
  35.  
  36. public function onHTTPStatus(status:HTTPStatusEvent):void{
  37. logEvent("    Status: " + status.status);
  38. }
  39.  
  40. public function onError(error:IOErrorEvent):void{
  41. logEvent("    Request failed. " + error.text);
  42. }
  43.  
  44. public function onSecurityError(error:SecurityErrorEvent):void{
  45. logEvent("    Request failed. " + error.text);
  46. }
  47.  
  48. public function logEvent(entry:String):void {
  49. log.text += entry + "\n";
  50. trace(entry);
  51. }
  52. ]]-->
  53. </mx:script>
  54. <mx:hbox width="100%">
  55. <mx:button label="Ping" id="pingButton" click="ping();" width="30%">
  56. <mx:textinput id="resourceURL" text="http://www.wowww.nl" width="70%">
  57. </mx:textinput>
  58. <mx:textarea id="log" width="100%" height="100%" editable="false">
  59. valueCommit="log.verticalScrollPosition=log.textHeight;"/&gt;
  60. </mx:textarea>
  61. [/XML]

Gepost in categorie: Social

Reacties

Plaats reactie zonder facebook account

Anoniem