如何从flex调用Web服务

时间:2012-03-28 20:15:09

标签: web-services flex flex4

我从flex调用web服务时遇到了一些问题。我有一个名为UserService的服务,其中包含一个方法字符串GetData(int i)。我想从flex调用此方法并获取数据。我的代码在这里:

                    protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            uService = new UserService();
            uService.addEventListener("hello", echoResultHandler);
            uService.GetData(1);                            
        }

        public function echoResultHandler(event:ResultEvent):void { 
            var retStr:String = event.result as String;                 
            var retInt:int = event.result.echoInt; 
            Alert.show('want to play', retStr);
        } 

可能我的问题并不困难,但我无法理解为什么它不起作用。有人可以帮助我吗?

服务代码,当我添加对servese的引用时由flex生成。

    internal class _Super_UserService extends com.adobe.fiber.services.wrapper.WebServiceWrapper
{


    public function _Super_UserService()
    {

        _serviceControl = new mx.rpc.soap.mxml.WebService();
        var operations:Object = new Object();
        var operation:mx.rpc.soap.mxml.Operation;

        operation = new mx.rpc.soap.mxml.Operation(null, "GetData");
         operation.resultType = String;
        operations["GetData"] = operation;

        _serviceControl.operations = operations;
        try
        {
            _serviceControl.convertResultHandler = com.adobe.serializers.utility.TypeUtility.convertResultHandler;
        }
        catch (e: Error)
        {  }


        preInitializeService();
        model_internal::initialize();
    }

    protected function preInitializeService():void
    {


        _serviceControl.service = "UserService";
        _serviceControl.port = "BasicHttpBinding_IUserService";
        wsdl = "http://localhost:3905/UserService.svc?wsdl";
        model_internal::loadWSDLIfNecessary();
    }

    public function GetData(value:int) : mx.rpc.AsyncToken
    {
        model_internal::loadWSDLIfNecessary();
        var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("GetData");
        var _internal_token:mx.rpc.AsyncToken = _internal_operation.send(value) ;
        return _internal_token;
    }

}

继承类:

public class UserService extends _Super_UserService
{

    protected override function preInitializeService():void
    {
       super.preInitializeService();
       // Initialization customization goes here
    }

}

1 个答案:

答案 0 :(得分:4)

您的UserService类永远不会调度名为“hello”的事件;因此,永远不会触发您的结果处理程序。我想你需要在ASynctoken中添加一个结果处理程序。

var call : Asynctoken =  uService.GetData(1);   
call.addResponder( new AsyncResponder(echoResultHandler) );

有关AsyncResponderAsyncToken

的更多信息