如何立即获取RemoteObject的结果?
我需要创建一个执行java方法并返回结果的函数。它是静态的,因为很多其他函数都会调用这个函数。所以我这样做了:
public static function FKDescription(dest:String):String{
var jRemote:RemoteObject = new RemoteObject();
var s:String;
jRemote.destination = dest;
jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
jRemote.getValues();
function valresult(event:ResultEvent):void{
s = event.result as String;
}
return s;
}
但是函数返回 null,因为 valresult() 没有在 main 函数末尾被调用。我应该怎么做才能使 FKDescription() 返回来自远程对象的字符串?
坦克。
I need to make a function that execute a java method and return his result. It is static becouse a lot of other functions will call this function. So I did this:
public static function FKDescription(dest:String):String{
var jRemote:RemoteObject = new RemoteObject();
var s:String;
jRemote.destination = dest;
jRemote.getValues.addEventListener(ResultEvent.RESULT,valresult);
jRemote.getValues();
function valresult(event:ResultEvent):void{
s = event.result as String;
}
return s;
}
But the function returns null, because the valresult() was not been called at the end of main function. What shoud I do to make FKDescription() return the string that came from remoteobject?
Tanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 HTTP 调用是异步的,所以你必须想办法获取结果。您想要做的是将结果处理程序删除到它自己的函数中,以便它等待结果,然后对其执行某些操作。 不可能做您现在想要完成的事情,即立即返回值。
请查看此处了解如何进行异步调用。
That's because HTTP calls are asynchronous, so you have to way for the result. What you want to do is remove the result handler to it's own function so that it waits for the result, then does something with it. It is NOT possible to do what you're trying to accomplish right now, which is returning the value right away.
Check here on how to do async calls.
正如 J_A_X 所说,所有 http 请求都是异步的,我建议以这种方式重构您的代码:
并且在调用者中,而不是:
您可以这样做:
as J_A_X said, all the http requests are async, i suggest to refactor your code this way:
and in the caller, instead of:
you can do this: