9 Replies Latest reply on Sep 5, 2013 2:11 AM by huangxinghui

    Reading web service results

    nakea Level 1

      I have a flex application that consumes a web service I wrote in .net.

       

      I'm having a hard time figuring out how to read the results of a CallResponder in my AS block, where I want to have some logic that makes a decision base on the result.

       

      -----------------------------

       

      Example:

       

      I have a webservice call (wsdl) that returns a Boolean response to whether an account exists based on an email and password passed in the call.

       

      If I add the service to the Data/Services section, then right-click to generate the servcie call using FlashBuilder's auto-generate feature, it creates CallResponder and the ResultHandler function.

       

      In the ResultHandler, It creates a CallResponder.token.

       

      I've tried to cast it as a Boolean and run it through an "if" statement, but it always comes up false.

       

      ----------------------------------

       

      I have the same problem just reading the result, which I understand is returned as an object wrapped around an ArrayCollection. How do I read the response of a webservice call that returns an  Array of string results.

       

      I've found a number of examples to simply bind to visual controls, which is great, but how do I read individual results to run some logic?

       

      Appreciate the help and sorry if this is such a basic request. I'm a newby to Flex/FlashBuilder.

        • 1. Re: Reading web service results
          pauland Level 4

          Have you looked here?

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_6.html

           

          If nothing else, use the debugger in the result handler to examine what the result is.

          • 2. Re: Reading web service results
            nakea Level 1

            Thanks Paul!

             

            That was very helpful and I'm getting close, though now I'm trying to figure out why there is a delay in getting my results.

             

            I have the fullowing function, but it takes two clicks for me to get the proper response:

             

             

            private var isItTrue:String;

            protected function button_clickHandler(event:MouseEvent):void

                                          {

              // Please uncomment the below line if Data Management is enabled for Credentials and IsRegistered is used as the create function.

              // var credentials:Credentials = new Credentials();

                                                    credentials.Email = emailTextInput.text;

                                                    credentials.Password = passwordTextInput.text;

             

                                                    IsRegisteredResult.token = users.IsRegistered(credentials);

              isItTrue = IsRegisteredResult.lastResult as Boolean;

             

              if(isItTrue == true){

                Alert.show('Success');

              }else{

                Alert.show('Failed');

               }

             

            }

             

            For some reason, the http call goes out, but I have to click the button for this function twice before it shows a positive result for a valid entry.

             

            Any ideas?

            • 3. Re: Reading web service results
              pauland Level 4

              You can't handle the result of the HTTP call inside the click handler - it's asynchronous.

               

              In the example I provided the link for there's a result handler for the http call: onResult

               

              That's where you handle the result - not in the click handler.

              • 4. Re: Reading web service results
                nakea Level 1

                Hi Paul,

                 

                I really appreciate your responses. I'm not sure why I'm not getting a handle on this. I tried to create an onResult function call and it still seams to be delayed. I'm calling it from within the click event, so it should run after the data is retrieved.

                 

                Would you be willing to look at the code I posted before your last response and suggest how I would achive my goal?

                 

                Thanks again for your helping me understand this.

                • 5. Re: Reading web service results
                  pauland Level 4

                  You DO NOT CALL the onResult function. It is called by the system when a result is received from the remote server.

                   

                  When you send the HTTP request to the remote server your code continues before the result is ready.

                   

                  You really need to spend some time understanding the differrence between synchronous and asynchronous events.

                  • 6. Re: Reading web service results
                    nakea Level 1

                    Hi Paul,

                     

                    You've been a great help. I read up on the asynchronous approach and I finally get it. I'm calling the http service adn from the service result calling and using the data.

                     

                    You may be able to help me figure one more thing out...

                     

                    Not that I'm getting the data, I want to part through the XML. I can use the ObjUtil to see the results, but when I try to call a single node and display it in and TextArea, I can't seen to get it to work.

                     

                      <s:WebService id="ws"

                                                                        wsdl="http://myservice.com/services/Notes.asmx?wsdl">

                                                  <s:operation name="GetList" resultFormat="e4x" result="showResult(event)" fault="operation1_faultHandler(event)" >

                                                            <s:request xmlns="">

                                                                      <email>{myEmail.text}</email>

                      </s:request>

                      </s:operation>

                      </s:WebService>

                     

                                                  protected function myButton_clickHandler(event:MouseEvent):void

                                                  {

                                                            ws.GetList.send();

                                                  }

                     

                                                  public function showResult(event:ResultEvent):void

                    {

                     

                                                            txtArea2.text = event.result.GetAdditionalUserData.Notes[1].Note;

                     

                                                  }

                     

                    Here is the actual results returned in the object:

                     

                    <GetListResponse xmlns="http://Nakea.net/TPI" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

                      <GetListResult>

                        <Notes>

                          <ID>17</ID>

                          <Email>jim@jim.com</Email>

                          <Date>2013-09-01T15:31:39.337</Date>

                          <Note>I'm looking to add a new note here</Note>

                        </Notes>

                        <Notes>

                          <ID>15</ID>

                          <Email>jim@jim.com</Email>

                          <Date>2013-08-29T19:14:01.383</Date>

                          <Note>This is my note</Note>

                        </Notes>

                        <Notes>

                          <ID>14</ID>

                          <Email>jim@jim.com</Email>

                          <Date>2013-08-29T19:14:00.197</Date>

                          <Note>This is my note</Note>

                        </Notes>

                      </GetListResult>

                    </GetListResponse>

                     

                     

                    I've also tried:

                     

                      txtArea2.text = event.result.Notes[1].Note;

                    • 7. Re: Reading web service results
                      huangxinghui Level 1

                      try to use event.result.GetListResult.Notes[1].Note

                      • 8. Re: Reading web service results
                        nakea Level 1

                        Hi HuangXingHui,

                         

                        I tried to return that before in an Alert:

                         

                        Alert.show(event.result.GetListResult.Notes[1].Note);

                         

                        It Didn't work. Just tried it again and same thing.

                         

                        Not sure why.

                        • 9. Re: Reading web service results
                          huangxinghui Level 1

                          because the xml namspace

                           

                          you define  xmlns="http://Nakea.net/TPI"

                           

                          so we should access the node in this namespace

                           

                          the code:

                           

                          public function showResult(event:ResultEvent):void

                          {

                              var resultXML:XML = event.result;

                              var defaultNS:Namespace = resultXML.namespace();

                              txtArea2.text = resultXML.defaultNS::GetListResult.defaultNS::Notes[1].defaultNS::Note;

                          }