I am in the process of evaluating Flex for a series of applications 
for a large financial services company. We intend to make significant 
use of Web Services. I have had trouble using the <mx:WebService> tag 
but not using the WebService API. Basically, the WebService API works 
and the tag doesn't. I am sure that I am missing something, being new 
to Flex, so I am asking for some guidance.

I carved up the FlexSDK sample called restaurant - I am using Adobe's 
web service, so, everyone should be able to copy the following code 
and experience the same issue.

In the following, pressing the "WebService (No Tag)" button 
successfully round-trips with the Adobe web service. Pressing 
the "WebService (Tag)" does not. I hope someone can tell me why.

Code:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";>

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;  
            import mx.rpc.soap.mxml.WebService;
                        import mx.rpc.AbstractService;
                        import mx.rpc.events.ResultEvent;
                        import mx.rpc.events.FaultEvent;
                import mx.rpc.AsyncToken;
                import util.TokenResponder;

            [Bindable]
                    private var restaurant:Object;
            [Bindable]
            private var description:String = new String("");

            public function doItNoTag(idString:String):void
            {
                var id:int = parseInt(idString);
                var token: AsyncToken = AsyncToken
(getRestaurantService().getRestaurant(id));
                token.addResponder(new TokenResponder
(getDescription, "Description Error"));
            }
                                
                    private function getDescription
(event:ResultEvent):void
                    {
                      if (event.result != null)
                      {
                        restaurant = event.result;
                        description = restaurant.description;
                      }
                      else
                      {
                        restaurant = null;
                      }
                    }
                                
                    private static var 
restaurantService:AbstractService;
                
                    public static function getRestaurantService
():AbstractService
                    {
                       if (restaurantService == null) 
                       {
                            var ws:WebService = new WebService();
                            ws.wsdl 
= "http://www.adobe.com/go/flex_restaurant_restaurant_service?wsdl";;
                            ws.useProxy = false;
                            ws.showBusyCursor = true;
                            ws.loadWSDL();
                            restaurantService = ws;
                        }
                        return restaurantService;
                    }
                  
        ]]>    
    </mx:Script>

    <mx:WebService id="WS" 
wsdl="http://www.adobe.com/go/flex_restaurant_restaurant_service?wsdl";
        fault="Alert.show(event.fault.faultString), 'Error'">

        <mx:operation name="getRestaurant" resultFormat="object">
            <mx:request>
                <restaurantId>{restaurantId.text}</restaurantId>
            </mx:request>
        </mx:operation>
    </mx:WebService>

    <mx:Panel title="WebService Test" height="75%" width="75%" 
        paddingTop="10" paddingBottom="10" paddingLeft="10" 
paddingRight="10">

        <mx:Label width="100%"  color="blue" text="Enter restaurant 
id:"/>
        <mx:TextInput id="restaurantId" text="53"/>
                <mx:Button label="WebService (No Tag)" 
click="doItNoTag(restaurantId.text)"/>
                <mx:Button label="WebService (Tag)" 
click="WS.getRestaurant.send()"/>
        <mx:TextArea width="100%" text="Restaurant Description: 
{description}"/>
        <mx:TextArea width="100%" text="Restaurant Description (Tag): 
{WS.getRestaurant.lastResult.Restaurant.description}"/>

    </mx:Panel>    
</mx:Application>

Code for the util.TokenResponder class (copied directly from the 
Adobe sample):

//////////////////////////////////////////////////////////////////////
//////////
//
// Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its 
licensors.
// All Rights Reserved.
// The following is Sample Code and is subject to all restrictions on 
such code
// as contained in the End User License Agreement accompanying this 
product.
// If you have received this file from a source other than Adobe,
// then your use, modification, or distribution of it requires
// the prior written permission of Adobe.
//
//////////////////////////////////////////////////////////////////////
//////////
package util
{

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

/**
 * A simple responder that will call the result function specified but
 * handles any fault by simply raising an Alert with the specified 
title.
 */
        public class TokenResponder implements IResponder
        {
            private var resultHandler:Function;
            private var faultTitle:String;
        
            public function TokenResponder(result:Function, 
faultTitle:String=null)
            {
                super();
                resultHandler = result;
                this.faultTitle = faultTitle;
            }
            public function result(data:Object):void
            {
                resultHandler(data);
            }
        
            public function fault(info:Object):void
            {
                //the info object from an AsyncToken is always a 
FaultEvent
                Alert.show(FaultEvent(info).fault.toString(), 
faultTitle);
            }
  }
}






--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to