HTTPService/ResultEvent with Flex 3.2 versus Flex

2019-05-16 08:44发布

问题:

through a design decission or what-so-ever Adobe changed the content of the ResultEvent fired by a HTTPService Object.

Take a look at following example:

var httpService:HTTPService = myHTTPServices.getResults();
httpService.addEventListener(ResultEvent.RESULT,resultHandler);
httpService.send();

/**
 * Handels the login process
 */
function resultHandler(event:ResultEvent):void
{
    // get http service
    var httpService = (event.target as HTTPService);

    // do something
}

It works like a charm with Flex 3.2. But when I try to compile it with Flex 3.5 or Flex 4.0 event.target as HTTPService is null.

I figured out that event.target is now an instance of HTTPOperation. That is interesting because I can't find HTTPOperation in the langref. However, I think what Flash Builder's debugger means is mx.rpc.http.Operation.

The debugger also shows that event.target has a private attribute httpService which is the instance I expected to get with event.target. But it's private, so event.target.httpService doesn't work.

If I only want to remove the EventListener I can cast event.target as EventDispatcher. But I need to use methods from HTTPService.

So: How can I get the HTTPService instance from the ResultEvent?

Any help would be appreciated. Thanks!

J.

回答1:

It is useful to go through the source if you get into this. On OS X the rpc classes are here: /Applications/Adobe Flash Builder Beta 2/sdks/3.4.1/frameworks/projects/rpc/src

Inside mx.rpc.http.HTTPService there is indeed an inner-class named HTTPOperation. It extends mx.rpc.http.AbstractOperation which in turn extends mx.rpc.AbstractOperation. Inside AbstractOperation is a getter method get service which looks to return what you need.

Since HTTPService is an inner-class it is effectively private so you'll need to cast to an AbstractOperation (either mx.rpc.http.AbstractOperation or mx.rpc.AbstractOperation).

So something like:

function resultHandler(event:ResultEvent):void
{
    // get the operation
    var operation:AbstractOperation = AbstractOperation(event.target);

    // get http service
    var httpService:HTTPService = HTTPService(operation.service);
}

edit: I take it back! Looks like Adobe is sending null for the service when it calls the super when constructing the HTTPOperation. The HTTPService is therefore only cached in the private variable httpService. I have no idea why they hide it from you but it looks like you'll have to keep your own reference around.



回答2:

I solved this problem for myself. There are some properties in HTTPService that are available from AbstractOperation. For example, I use property request which is an Object:

myService.request["service"] = myService;

And later, when I get Event which has HTTPOperation in event.currentTarget, I get my HTTPService in such way:

 var eventService : HTTPService = HTTPService( AbstractOperation( event.currentTarget ).request["service"] );