質問

I'm trying to develop a Flex Mobile / PHP application, and I'm running into some trouble with the AsyncToken... Does it not just return a basic string?

For example... I'm wanting to just return a simple string result from my request - right now, it's going to return a basic output string from the method that's implemented. The backend part works (PHP), I've done all of that... It's this that's giving me some problems:

import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function button_clickHandler(event:MouseEvent):void
{
    loginResult.token = user_service.login();
    loginResult.token.addResponder(new AsyncResponder(onResult,onFault));
}

public function onResult(event:ResultEvent, token:Object):void
{
    // what would i do here??
}

public function onFault(event:FaultEvent,token:Object):void
{
    txtResult.text = event.fault.faultDetail;
}

It's pretty straightforward - any suggestions, or recommendations would surely help! Thank you!

役に立ちましたか?

解決

Easy enough. The ResultEvent#result property contains the value returned by the remote service. All you need to do is cast it to the right type (since it is an Object by default).

For instance, handling your service method that returns a String:

public function onResult(event:ResultEvent):void {
    var s:String = event.result as String;
}

Same goes for other types:

var a:Array = event.result as Array;

Or even more complex custom classes:

var instance:MyClass = event.result as MyClass;

Note that this last example will only work with AMF remoting; you have to have the same class definition on the client side and on the server side; and you have to let them know of each other's existence by using the [RemoteClass(alias="net.riastar.MyClass")] metadata tag on your AS class definition. How you have to handle this on the server side depends on the language used there.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top