6 Replies Latest reply on Sep 27, 2006 11:37 AM by kriskross

    Using WebService from AS 3.0

    kriskross
      Hi.
      I'm trying to create a AS calss that uses a webService. I'm having troble finding some info about this.
      I've got four simple methods in my java webservice:
      Hello() returns String;
      Hello(String name) returns String;
      TestObject() returns testobject;
      getTestObjects() returns List<TestObject>;

      I can't figure out how to get any result from the web services. I've tried the folloving code, but all I get is : result[object Operation].

      private var ws:WebService;

      private function initAll():void {
      ws = new WebService();
      ws.wsdl = " http://localhost/developmentWebSService/developmentWebS?WSDL";
      ws.loadWSDL();
      mx.controls.Alert.show("result" + ws.SayHello);
      }

      Hope someone can helpe me,
        • 1. Re: Using WebService from AS 3.0
          MichaelMartin77
          First, in order to invoke a remote web service method, you need to use the send() method. In your case you need the following line of code:

          ws.SayHello.send().

          Due to the asynchronous nature of the WebService component, the lastResult object is not populated immediately after the call to a web service method. You need to wait for the webservice to return data and then trap that event. So, you need to add an event handler for the webservice's result event. In the event handler, you can access the result object from your method call as follows:

          ws.SayHello.lastResult;

          The lastResult property will vary depending on exactly what your web service returns to you. By default, Flex will convert the result into an generic object. If the remote method only returns a single record, you can use the dot syntax to access the content of any tags in the result as follows:

          ws.SayHello.lastResult.someTagName;

          However, if the remote method returns more than one record, you must access it this way:

          ws.SayHello.lastResult[0].someTagName;

          If you are using Flex Builder, the best way to determine the structure of the lastResult object is to set a breakpoint inside the result event handler. The go to the variables tab, scroll down to the ws object, expand it, and look at the lastResult object's properties.
          • 2. Re: Using WebService from AS 3.0
            Kristian Wright Level 1
            Is there anyway to pause the processing of an application until the result of the webservice has been passed back?
            • 3. Re: Using WebService from AS 3.0
              MichaelMartin77 Level 1
              Not that I know of. But, you can setup an event handler to handle to result event for an individual operation within a webservice. So you can create a function like this and bind it to the operation's result event.

              private function SayHelloWorld_Result():void
              {
              //Handle result of method call here
              }

              Webservice method calls are asynchronous. It is actually to your advantage that the program doesn't halt execution until they return a result. Basically, the user requests the data from a webservice and then goes on their merry way doing whatever else they need to in the application until the webservice returns a result. Depending on connection speed, your user could wait up to 15 or 20 seconds on that result (usually it is much less).

              • 4. Re: Using WebService from AS 3.0
                kriskross Level 1
                Hi.
                Tnx for answer. Here is my mx:application. I now get an error: "WSDLError faultString="Element http://developmentWS/:SayHello2 not resolvable" faultCode="WSDL.BadElement" faultDetail="null"] " That I can't figure out.

                Can you see what I do wrong?

                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" >
                <mx:Script>
                <![CDATA[
                import mx.rpc.soap.WebService;
                import mx.rpc.events.FaultEvent;
                import mx.rpc.events.ResultEvent;
                import mx.controls.Alert;

                public var ws:WebService;

                private function test():void {
                ws = new mx.rpc.soap.WebService();
                ws.wsdl = " http://localhost/developmentWebSService/developmentWebS?WSDL";
                ws.SayHello2.addEventListener("result", SayHello_Result);
                ws.loadWSDL();
                ws.SayHello2.send();
                }

                public function SayHello_Result():void {
                Alert.show(ws.SayHello2.lastResult);
                }

                ]]>
                </mx:Script>
                <mx:Button x="107" y="203" label="test as:WebService" click="{test()}"/>
                </mx:Application>
                • 5. Re: Using WebService from AS 3.0
                  MichaelMartin77 Level 1
                  Is that a .NET Web Service? I noticed that your URL seems to potentially be missing some elements? You posted the following URL for the WSDL page of your web service:

                  http://localhost/developmentWebSService/developmentWebS?WSDL

                  Double check and make sure it should not have a .asmx right before the question mark like this:

                  http://localhost/developmentWebSService/developmentWebS.asmx?WSDL
                  • 6. Re: Using WebService from AS 3.0
                    kriskross Level 1
                    No, it's not a .NET Web Service!

                    Java webservice using latest glassfish server.