Flex AIR --  Problem in Internet Connectivity

Posted on 2011-03-15
Medium Priority
Last Modified: 2012-05-11
Dear Sir/Madam,

      we are developing a product using Flex AIR in which we are using web services to connect to the server.
All the services we are calling in the main screen and the result is handling in all other screens.
And we are using a 3G connection for Internet. If there is no Internet connection in the middle of the screen, we again need to go back to the main screen to active the services. Is there any way to make the services up if there is an internet is up.

Calling some of the services at the application start up. How feasible is this.

Any help is appreciable.

Thanks & Regards
Question by:SreeramojuPradeep
  • 3
  • 3
  • 2
LVL 17

Expert Comment

ID: 35144952
If your application is kept open you can check for internet connectivity by running a Timer.
For checking connection try this:
private var networkAvailable : Boolean = false;

private function checkNetworkConnection() : void
	var request:URLRequest = new URLRequest();
	request.method = "HEAD";
	request.url = "http://www.google.com"; 
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, connectHttpStatusHandler );
	urlLoader.addEventListener( IOErrorEvent.IO_ERROR, connectErrorHandler ); 
	urlLoader.load( request); 
private function connectHttpStatusHandler( event:HTTPStatusEvent):void
	networkAvailable = ( event.status == 0 ? false : true );
private function connectErrorHandler(event:IOErrorEvent):void
	networkAvailable = false;
private function handleNetworkChange() : void
	if( networkAvailable ) {
		trace( "Online" );
	} else {
		trace( "Offline" );

checkNetworkConnection();//call this function on timer

Open in new window

Warm Regards
Deepanjan Das
LVL 18

Expert Comment

ID: 35145082
You can have a simple webservice that just returns true

such as checkInternetConectivity

thus if this returns true you have a connection, you catch the fault event, and then know you have no internet, then you can allow them to retry or work offline or quit.
LVL 17

Expert Comment

ID: 35145176
@Plucka is right, here is the tutorial - http://enginyoyen.com/blog/eng/checking-http-connection-with-air-flash-flex/

It is not a webservice but a swc library in the air package.

Warm Regards
Deepanjan Das
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.


Author Comment

ID: 35145930
Hi All,

I would like to explain my problem with some code snippet....

I am calling some web services initially on applicationComplete i.e init() method on startUp of application

On creationComplete i am calling networkCheck() method,

I have set the poll interval also but the event is getting triggered only once...

If there is no internet connection on app startup..the services called in init() method will not be called again....so the result will be null....
But once the internet connection is up i should call the init() method.....i.e call the services in the init method.....
can we call this init() method once the monitor.available = true;
To do this i have set the pool interval but it is not triggered...
How feasible is it calling init() method when monitor.available=true...if suppose pollinterval works properly also...

<s:WindowedApplication  xmlns:fx="http://ns.adobe.com/mxml/2009" 
 						xmlns:Components="Components.*" showStatusBar="false"
						applicationComplete="init()" xmlns:local="*" creationComplete="networkCheck();" width="1024" height="600" >


		private var monitor:URLMonitor;
		/*Set up the URL string variable for the URLRequest to check. In this example, let's use google.com as we can be sure 99.99% 
		of the time that it will be up and running. */ 
		//private var strURLMonitor:String = 'http://www.google.com'; 
		// URLRequest that the Monitor Will Check
		private	var urlReq:URLRequest = new URLRequest("http://www.google.com");
		protected function init():void
			//Reference get xmlPath for buttons
			getXmlPathLinkReferenceResult.token = xmlPathCommunicationPort.getXmlPathLink("references", "bare acts");
			getXmlPathLinkJudgementsResult.token =  xmlPathCommunicationPort.getXmlPathLink("references", "judgments");
			getXmlPathLinkAuthoritiesResult.token = xmlPathCommunicationPort.getXmlPathLink("references", "authorities");
			getXmlPathLinkBillsResult.token = xmlPathCommunicationPort.getXmlPathLink("references", "bills");
			getXmlPathLinkOrdinanceResult.token = xmlPathCommunicationPort.getXmlPathLink("references", "ordinances");
			getXmlPathLinkNotificationsResult.token = xmlPathCommunicationPort.getXmlPathLink("references", "notifications");
		// network monitoring functions 	
		private function networkCheck():void {      
			/*  Keep an eye on network connectivity... 
			Any losses, and run the onNetworkChange method */ 
			NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange); 
			// run monitorConnection 
		private function monitorConnection():void {
			// Checks Only the Headers - Not the Full Page
			monitor = new URLMonitor( urlReq); 
			monitor.addEventListener( StatusEvent.STATUS,announceStatus); 
			// Set the Interval (in ms) - 3000 = 3 Seconds
			monitor.pollInterval = 3000; 
		private function announceStatus(e:StatusEvent):void { 
			trace("Status change. Current status: " + monitor.available); 
			if(monitor.available) { 
				/* init(); */
				trace("Connection available");
				/* Inside here, you can run any functions or script 
				that require network connection, as it exists. */ 
			} else { 
				Alert.show("Remote Server Not Reachable, Please Check Your Internet Connection","Sorry");
		// Handles when the HTTP Network status has changed.
		protected function onStatusChange(e:StatusEvent):void
			if (monitor.available) 
				trace("Network is online");
			} else {
				trace("Network is offline");
		/* The network connection has changed. Run the monitorConnection function to check status */ 
		private function onNetworkChange(event:Event):void { 
			trace('network change'); 


Open in new window

LVL 18

Accepted Solution

Plucka earned 500 total points
ID: 35146736

init is just a normal method.

What you should probably do is in creation complete

just check the network connectivity.

Once that is valid, then call init.

If its not valid, setup a timer that checks every X seconds if you want the system to automatically check
LVL 18

Expert Comment

ID: 35146752
looking at the docs I dont see that pollinterval is supported thus my suggestion to run a timer

Author Closing Comment

ID: 35147079

Author Comment

ID: 35147801
Hi All,

I implemented in the way u suggested but i have problem in the following code snippet.....

Pls let me know where i am wrong.......



//in this page i am using the result of the services i.e on init() of page1.mxml.....

//I am using the below code in page1.mxml init() method 

//If initially the internet connection is not available and later if the connection is available also the result is null....

I am using FlexGlobals.toplevelApplication.getXmlPathLinkReferenceResult.lastResult...in order to get the result but the result is null if the internet connection is available.....this problem occurs only if on application startup the internet connection fails....

private function init():void {
			//get base path for file download
			var bareActsXmlPathResult:String;
			bareActsXmlPathResult=FlexGlobals.topLevelApplication.getXmlPathLinkReferenceResult.lastResult; //get xmlPath for reference Btn
		    var judgementsXmlPathResult:String; 
			judgementsXmlPathResult=FlexGlobals.topLevelApplication.getXmlPathLinkJudgementsResult.lastResult;//get xmlPath for judgements Btn
			var authoritiesXmlpathResult:String;
			authoritiesXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkAuthoritiesResult.lastResult;//get xmlPath for authorities Btn
			var billsXmlpathResult:String;
			billsXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkBillsResult.lastResult;//get xmlPath for bills Btn
			var ordinaceXmlpathResult:String;
			ordinaceXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkOrdinanceResult.lastResult;//get xmlPath for bills Btn
			var notificationsXmlpathResult:String;
			notificationsXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkNotificationsResult.lastResult;//get xmlPath for notifications Btn

Open in new window


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
I’m a fan of folder redirection, however, it does have a couple of “Gotchas!” you have to look out for.  For example, if you redirect a user’s AppData folder to a DFS namespace, shortcuts on the taskbar are no longer trusted.  Here’s how to fix that.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question