2 Replies Latest reply on Mar 17, 2008 11:53 AM by ntsiii

    XML and Web Services

    Pocushocus Level 1
      Hi !

      I have a problem using web services... I have the next action script class:

      package cwdi.clientws
      {
      import mx.rpc.soap.WebService;
      import mx.rpc.soap.LoadEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.Fault;
      import mx.controls.Alert;


      public class ClientWS
      {

      //private var CONSTANTES : CONSTANTS = new CONSTANTS();
      private var webService : WebService = new WebService();
      private var action: String = new String();
      private var xmlQuery:String = new String();
      public var xmlResponse:String;



      //public var xmlQuery:String = new String(query.generateXMLQueryCategorias("holas",15));
      public function loadWebService (url_wsdl:String, action:String, xmlQuery:String) : void
      {
      // Cargar el WSDL y agregar listeners para saber cuando este listo


      this.action = action;
      this.xmlQuery = xmlQuery;

      webService.wsdl = url_wsdl;

      webService.addEventListener(LoadEvent.LOAD, onWSDL);

      webService.addEventListener(FaultEvent.FAULT, onWebServiceFault);

      webService.loadWSDL();
      }

      //listener que avisa cuando el WSDL se ha cargado


      private function onWSDL ( event : LoadEvent ) : void
      {
      // Llamada a un metodo en el WebService, primero se crea otro listener para saber cuando
      // el resultado esta listo.
      webService.executeActionOnCategorias.addEventListener(ResultEvent.RESULT, getResult);
      mx.controls.Alert.show("onWSDL");
      webService.executeActionOnCategorias(this.action,this.xmlQuery);
      }



      public function getResult ( event : ResultEvent ) : void
      {
      // Usar el resultado. Para mas detalles se puede inspecionar event.result en el debugger.
      //textArea.text += "La temperature maxima para hoy en " + event.result.PlaceName + " , "+ event.result.StateCode + " es : " + event.result.Details[0].MaxTemperatureC + "C";
      //var x= new XML(event.result.toString());

      this.xmlResponse=event.result.toString();
      //setXmlResponse(event.result.toString());
      mx.controls.Alert.show(this.xmlResponse+" getResult");
      //this.miCombo.dataProvider=x.option;

      }

      // En caso de que el WebService no puede ser cargado.

      private function onWebServiceFault(event:FaultEvent ):void
      {
      var fault: Fault = event.fault;
      mx.controls.Alert.show("onWebServiceFault");
      var message:String = "An error occurred. The details are as follows\ncode: " + fault.faultCode;

      message += "\ndetail: " + fault.faultDetail;
      mx.controls.Alert.show("onWebServiceFault");
      trace("Web Service Error :" + message);
      }
      public function getXmlResponse():String
      {
      mx.controls.Alert.show(this.xmlResponse+"getXmlResponse");
      return this.xmlResponse;

      }
      public function setXmlResponse(xmlRespon:String):void
      {
      mx.controls.Alert.show(xmlRespon+"setXmlResponse");
      this.xmlResponse=xmlRespon;

      }

      }}


      but when y recover the xmlResponse var at the MXML and the xmlResponse is equal NULL... Why? Could you help me please! I thought that ActionSript is similar to JAVA and the xmlResponse var must contains the response String...

      Help me please!
        • 1. Re: XML and Web Services
          SujitG Level 2
          Hi,
          If the object instance invoking the web service and the object instance on which you are reading the xmlResponse are same, then the xmlResponse string should contain the value. Can you make sure if you are trying to access the xmlResponse variable after the web service response is received. i.e access the xmlResponse object after the getResult() method is invoked by the Web Service component.
          Hope this helps.
          • 2. Re: XML and Web Services
            ntsiii Level 3
            1) unless you have a good reason to choose otherwise, you should set the resultFormat property of the WebService operation/method equal to "e4x" If you do not, then Flex converts your XML into a nested dynamic object structure. There is alnost no benefit to this.

            instead, set resultFormat="e4x". then in the result handler function, do:
            public function getResult ( event : ResultEvent ) : void
            {
            var xmlResult:XML = event.result as XML;
            trace(xmlResult.toXMLString());

            Do this first to make sure you aer getting your data.

            Also, it is probably a mistake to name a string variable xmlResponse. The xml prefix implies that variable contains XML

            Tracy