Solved

Flex AIR --  Problem in Internet Connectivity

Posted on 2011-03-15
8
753 Views
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
Pradeep
0
Comment
Question by:SreeramojuPradeep
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Expert Comment

by:deepanjandas
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 );
	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
 
LVL 18

Expert Comment

by:Plucka
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.
0
 
LVL 17

Expert Comment

by:deepanjandas
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
0
 

Author Comment

by:SreeramojuPradeep
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: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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 18

Accepted Solution

by:
Plucka earned 125 total points
ID: 35146736
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
 
LVL 18

Expert Comment

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

Author Closing Comment

by:SreeramojuPradeep
ID: 35147079
.
0
 

Author Comment

by:SreeramojuPradeep
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.......

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now