using flex web service
Hi,
I was plying around how to load flex webservice there are many way to do but I was looking
some native action script class which can response me back once a webservoce loaded
successfully or if any fault event executes.
Idecided to write my own custom class which can load a wsdl and I feel more easy
to use this class instead of using mxml tag every time.
This class will execute one httpservice at a time you have to make it sync
package com.sanjeev.net.core.io {
import mx.core.EventPriority;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.LoadEvent;
import mx.rpc.soap.mxml.Operation;
import mx.rpc.soap.mxml.WebService;
/**
* The WsdlClient provides means by which Insight can communicate with
* Presentation Server
*/
public class WsdlClient {
private static var _instance: WsdlClient = null;
private var _service : WebService = null;
private var _isServiceLoaded : Boolean = false;
private var _opp : String = null;
private var _params : Array = null;
private var _wsdl : String = null;
// pass this if calling same wsdl form two or more class at same time
private var _clsRef : Object = null;
private const REQUEST_TIME_OUT : int = 1000;
private const SERVER_URL : String = http://www.yourservername.com
/**
* @public
* @param – [Nothing]
* @return – [Object = WsdlClient]
* @des – returns an instance of the WsdlClient
*/
public static function get instance() : WsdlClient {
if (_instance == null) {
_instance = new WsdlClient();
}
return _instance;
}
/**
* @public
* @param – [String - wsdl name and method, Function - load handler, Function - fail handler,
* Array - wsdl method/opperation parameters, Object - reference for specific and new requets]
* @return – [Object = WsdlClient]
* @des – returns an instance of the WsdlClient
*/
public function invokeWebService(_serviceAndMethodName : String,
successHandler : Function,
failHandler : Function,
additionlParams : Array = null,
classRef : Object = null) : void {
//you will pass _serviceAndMethodName = “Search,getEmployee”
var wsdl : String = SERVER_URL + _serviceAndMethodName.split(’,’)[0];
var clsRef : Object = classRef;
_opp = _serviceAndMethodName.split(’,’)[1];
_params = additionlParams;
if(_wsdl != wsdl || _clsRef != clsRef){
_service = null;
_service = new WebService();
}
_service.requestTimeout = REQUEST_TIME_OUT;
_service.wsdl = wsdl;
_service.useProxy = false;
var operation : Operation = new Operation(_service, _opp);
operation.resultFormat = ‘e4x’;
if(_wsdl != wsdl || _clsRef != clsRef){
_service.addEventListener(LoadEvent.LOAD, handleServiceLloaded, false, EventPriority.DEFAULT, true);
_service.addEventListener(FaultEvent.FAULT, failHandler, false, EventPriority.DEFAULT, true);
_service.addEventListener(ResultEvent.RESULT, successHandler, false, EventPriority.DEFAULT, true)
_service.loadWSDL();
_wsdl = wsdl;
_clsRef = clsRef;
}
else{
//—service is already loaded———-;
_service.getOperation(this._opp).addEventListener(FaultEvent.FAULT, failHandler, false, EventPriority.DEFAULT, true);
_service.getOperation(this._opp).addEventListener(ResultEvent.RESULT, successHandler, false, EventPriority.DEFAULT, true)
this.send() ;
}
}
/**
* @private
* @param – [Event - LoadEvent]
* @return – [Nothing]
* @des – wsdl load handler
*
* */
private function handleServiceLloaded(event : LoadEvent) : void {
// wsdl was loaded ok, we can now do this:
this.send();
}
/**
* @private
* @param – [Nothing]
* @return – [Nothing]
* @des – send the requested wsdl oppration/method parameters
*
* */
private function send() : void {
if(_params != null){
_service.getOperation(this._opp).arguments = _params;
}
_service.getOperation(this._opp).send();
}
}
There is a problem with flex if you compile you application using flash player 10 and if you are sending some parameters via flex web service class then web service class does not decode string properly while sending to back end server there is a patch given by adobe see https://bugs.adobe.com/jira/browse/SDK-18326
to use this class you need to change send() method as below
private function send() : void {
if(_params != null){
_service.getOperation(this._opp).arguments = _params;
}
_service.xmlSpecialCharsFilter = function(value:Object):String {return value.toString()};
_service.getOperation(this._opp).send();
}
enjoy flex