Flex AIR -- Problem in Internet Connectivity

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
Pradeep
SreeramojuPradeepAsked:
Who is Participating?
 
PluckaConnect With a Mentor Commented:
Yes,

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
0
 
deepanjandasCommented:
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 );
	handleNetworkChange();
}
			
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
0
 
PluckaCommented:
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.
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
deepanjandasCommented:
@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
0
 
SreeramojuPradeepAuthor Commented:
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:s="library://ns.adobe.com/flex/spark" 
					   	xmlns:mx="library://ns.adobe.com/flex/mx" 
 						xmlns:Components="Components.*" showStatusBar="false"
						applicationComplete="init()" xmlns:local="*" creationComplete="networkCheck();" width="1024" height="600" >

<fx:Script>
		<![CDATA[

		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 
			monitorConnection(); 
		} 	
		
		private function monitorConnection():void {
			// Checks Only the Headers - Not the Full Page
			urlReq.method="HEAD";
			monitor = new URLMonitor( urlReq); 
			monitor.addEventListener( StatusEvent.STATUS,announceStatus); 
			// Set the Interval (in ms) - 3000 = 3 Seconds
			monitor.pollInterval = 3000; 
			monitor.start(); 
		} 	
		
		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'); 
			monitorConnection(); 
		} 
		
		]]>
	</fx:Script>

</s:WindowedApplication>

Open in new window

0
 
PluckaCommented:
looking at the docs I dont see that pollinterval is supported thus my suggestion to run a timer
0
 
SreeramojuPradeepAuthor Commented:
.
0
 
SreeramojuPradeepAuthor Commented:
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.......

Thanks

page1.mxml

//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
				if(bareActsXmlPathResult!=null)
					getBareActsXml(bareActsXmlPathResult);
		    var judgementsXmlPathResult:String; 
			judgementsXmlPathResult=FlexGlobals.topLevelApplication.getXmlPathLinkJudgementsResult.lastResult;//get xmlPath for judgements Btn
				if(judgementsXmlPathResult!=null)
					getJudgementsXml(judgementsXmlPathResult);
			var authoritiesXmlpathResult:String;
			authoritiesXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkAuthoritiesResult.lastResult;//get xmlPath for authorities Btn
				if(authoritiesXmlpathResult!=null)
					getAuthoritiesXml(authoritiesXmlpathResult);
			var billsXmlpathResult:String;
			billsXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkBillsResult.lastResult;//get xmlPath for bills Btn
				if(billsXmlpathResult!=null)
					getBillsXml(billsXmlpathResult);
			var ordinaceXmlpathResult:String;
			ordinaceXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkOrdinanceResult.lastResult;//get xmlPath for bills Btn
				if(ordinaceXmlpathResult!=null)
				    getOrdinanceXml(ordinaceXmlpathResult);
			var notificationsXmlpathResult:String;
			notificationsXmlpathResult=FlexGlobals.topLevelApplication.getXmlPathLinkNotificationsResult.lastResult;//get xmlPath for notifications Btn
			    if(notificationsXmlpathResult!=null)
					getNotificationsXml(notificationsXmlpathResult); 
		}

Open in new window

0
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.

All Courses

From novice to tech pro — start learning today.