Link to home
Start Free TrialLog in
Avatar of SreeramojuPradeep
SreeramojuPradeepFlag for India

asked on

Adobe Flex AIR--- How to call WSDL when Internet goes Down

Hi,

    In my Desktop Application, I have connected the WSDL to the server using the DataServices..............
Here is the WSDL am using...  http://121.243.253.109:8080/TestService/TestClassPort?wsdl

and my Code is as below..........
 
<?xml version="1.0" encoding="utf-8"?>
<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" 
					   creationComplete="checkNetworkConnection()"
					   xmlns:testclassport="services.testclassport.*"
					   >
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
			
			private var networkAvailable : Boolean = false;
			private var timer:Timer = new Timer(5000);
			
			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 ) {
					init();
					trace( "Online" );
				} else {
					
					timer.addEventListener(TimerEvent.TIMER,recheckInternetConnection);
					timer.start();
					trace( "Offline" );
				}
			}
			private function recheckInternetConnection(event:TimerEvent):void{
				timer.removeEventListener(TimerEvent.TIMER,recheckInternetConnection);
				checkNetworkConnection();
			}
			private function init():void{
				getTestResult.token = testClassPort.getTest();
			}
			[Bindable]
			private var getStr:String;
			protected function testClassPort_resultHandler(event:ResultEvent):void
			{
				getStr =  event.result.toString();
				
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<s:CallResponder id="getTestResult"/>
		<testclassport:TestClassPort id="testClassPort" 
									 result="testClassPort_resultHandler(event)"
									 fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
		
	</fx:Declarations>
	
	<mx:Label id="str" text="{getStr}" />
	
</s:WindowedApplication>

Open in new window


Initially if the Internet Connection is there, I can access the data from the server and it works fine.....
If the internet connection is not there Intially,  the below values are becoming null in the service package. HowEver the I have placed the Timer to check the Internet Connection but its not working when Internet Connection is not there Initially.


 _serviceControl.service = "TestClassService";
        _serviceControl.port = "TestClassPort";
        wsdl = "http://121.243.253.109:8080/TestService/TestClassPort?wsdl";



Avatar of dgofman
dgofman
Flag of United States of America image

You may need to implement reconnect button or popup login window for reestablish a server session.
Avatar of SreeramojuPradeep

ASKER

However i have set the timer to check internet connection....so when the connection is up it automaticaaly calls the services...so what is the the point of reconnect button...
For example you are using Java as webserver. By default Java stores user session maximum 30 minutes. So, your timer will ping dead connection every 1000 ms, but user will not able to continue working on your website, until pass authentication. If you are doing test application, you can use your approach but in commercial applications we are using user session
But we are developing desktop application pertaining to individuals.....
So using  sessions????...
Handling the same when no internet connection on application start up the wsdl services will not be called...even if timer is placed ......



No, doesn't matter you are using AIR, Browser SWF, JavaScript on backend JSP, Java, Java Servlets always creating user session

http://sujitreddyg.wordpress.com/2008/05/16/session-data-management-in-flex-remoting/
Hi,

 I agree with ur session concept ......

But i am calling web services at the application startup itself...when net conection is down on startup i am not able to call any web services (WSDL) once the net connection is up...
I have sent my appln source code via mail to make u clear what problem actually i am facing....
Can u pls let me know where i go wrong...


 
Hi Gofman,

   You told me to use loadWSDL(uri:String = null):void .......

Here in Flash builder IDE, am selecting Data from Main menu then am going into Connect to Data/Services
from there if I give the WSDL, automatically a package is generating
Here is the packages that is generated

_Super_TestClassPort.as

/**
 * This is a generated class and is not intended for modification.  To customize behavior
 * of this service wrapper you may modify the generated sub-class of this class - TestClassPort.as.
 */
package services.testclassport
{
import com.adobe.fiber.core.model_internal;
import com.adobe.fiber.services.wrapper.WebServiceWrapper;
import com.adobe.serializers.utility.TypeUtility;
import mx.rpc.AbstractOperation;
import mx.rpc.AsyncToken;
import mx.rpc.soap.mxml.Operation;
import mx.rpc.soap.mxml.WebService;

[ExcludeClass]
internal class _Super_TestClassPort extends com.adobe.fiber.services.wrapper.WebServiceWrapper
{
     
    // Constructor
    public function _Super_TestClassPort()
    {
        // initialize service control
        _serviceControl = new mx.rpc.soap.mxml.WebService();
        var operations:Object = new Object();
        var operation:mx.rpc.soap.mxml.Operation;

        operation = new mx.rpc.soap.mxml.Operation(null, "getTest");
        operation.resultElementType = String;
        operations["getTest"] = operation;

        _serviceControl.operations = operations;
        try
        {
            _serviceControl.convertResultHandler = com.adobe.serializers.utility.TypeUtility.convertResultHandler;
        }
        catch (e: Error)
        { /* Flex 3.4 and eralier does not support the convertResultHandler functionality. */ }



        _serviceControl.service = "TestClassService";
        _serviceControl.port = "TestClassPort";
		_serviceControl.wsdl = "http://121.243.253.109:8080/TestService/TestClassPort?wsdl";
		_serviceControl.loadWSDL();
		model_internal::loadWSDLIfNecessary();


        model_internal::initialize();
    }

    /**
      * This method is a generated wrapper used to call the 'getTest' operation. It returns an mx.rpc.AsyncToken whose 
      * result property will be populated with the result of the operation when the server response is received. 
      * To use this result from MXML code, define a CallResponder component and assign its token property to this method's return value. 
      * You can then bind to CallResponder.lastResult or listen for the CallResponder.result or fault events.
      *
      * @see mx.rpc.AsyncToken
      * @see mx.rpc.CallResponder 
      *
      * @return an mx.rpc.AsyncToken whose result property will be populated with the result of the operation when the server response is received.
      */
    public function getTest() : mx.rpc.AsyncToken
    {
        model_internal::loadWSDLIfNecessary();
        var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("getTest");
        var _internal_token:mx.rpc.AsyncToken = _internal_operation.send() ;

        return _internal_token;
    }
     
}

}

Open in new window


where I need to use loadWSDL() so that I can access the services once the Internet is up.......
TestClassPort.as

/**
 * This is a generated sub-class of _TestClassPort.as and is intended for behavior
 * customization.  This class is only generated when there is no file already present
 * at its target location.  Thus custom behavior that you add here will survive regeneration
 * of the super-class. 
 **/
 
package services.testclassport
{

public class TestClassPort extends _Super_TestClassPort
{

               
}

}

Open in new window

Try comment your services from MXML

<!--testclassport:TestClassPort id="testClassPort"
                                                       result="testClassPort_resultHandler(event)"
                                                       fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/-->

and create using ActionScript

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import services.testclassport.TestClassPort;

private function init():void{
      var testClassPort:TestClassPort = new TestClassPort();
      testClassPort.showBusyCursor = true;
      testClassPort.addEventListener(ResultEvent.RESULT, testClassPort_resultHandler);
      testClassPort.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void{
            Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)
      });
      getTestResult.token = testClassPort.getTest();
}

Is the same as MXML
This solved my problem....

But when net connection is disabled after calling  init() method...how to handle the net connectivity in such cases....is there  any way in which we can handle this issue of calling different  web services.....
Internet connectivity becomes major issue at this point.. how to handle ?????/
Can the above issue handled...since mine is a air application handling checking remote server reachable  is very important....
The solution u gave was very much ok...
But if server is not reachable in between the init() method....

Any alternative way to be used??????????
ASKER CERTIFIED SOLUTION
Avatar of dgofman
dgofman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial