Making HTTPRequest and getting response (Adobe Flex)

て烟熏妆下的殇ゞ 提交于 2019-12-24 14:42:57

问题


Im trying to make a HTTP Request in Adobe Flex (Actionscript) as follows:

var p:PersonSearchController = new PersonSearchController();
showAlertDialog();
p.search(sc);
alert.cancel();

navigator.pushView(views.PersonSearchResults, +p.getResp());

So basically, before the search we get a "Searching..." AlertDialog box, once the search is complete, the dialog box disappears and the results screen is pushed onto the screen...

Here is the search method:

function search{
var requestSender:URLLoader= new URLLoader();

dispatcher.addEventListener(Event.COMPLETE, completeHandler);

var urlRequest :URLRequest = new URLRequest("http://airpoint05:8888/MPS2/PersonSearch");

var msg:String = "blah";

/* Setup HTTP Request */
urlRequest.data = msg;
urlRequest.contentType = "application/x-www-form-urlencoded";
urlRequest.method = URLRequestMethod.POST;
requestSender.load(urlRequest);
}

And here is the completeHandler function:

/* URL has completed and got a response */
private function completeHandler(event:Event):void
{
    var response:URLLoader = URLLoader(event.target);
    this.res = URLLoader(event.target).data;
    trace(this.res);
    response.close();   
}

When this line is called: navigator.pushView(views.PersonSearchResults, +p.getResp());

p.getResp() is nothing as the response hasn't came back yet. I want the program to basically block until the HTTPResponse is received so I can process the results. At the moment the Popup appear and disappears quickly, and in the background the search goes off and makes the request... I get the response but only after the results screen has been pushed out. How can I make the popup block until we have a HTTPresponse?

Thanks Phil


回答1:


Don't use URLLoader for this, use HTTPService:

<fx:Script>
<![CDATA[
private function search(text:String):void
{
service.send({search:text}); // your service will receive the variable 'search' with your string
}

private function resultHandler(e:ResultEvent):void
{
var data:Object = e.result;
// do whatever else here
}
]]>
</fx:Script>
<s:HTTPService id="service" method="POST" url="http://airpoint05:8888/MPS2/PersonSearch" result="resultHandler" />



回答2:


HTTPService/URLRequest (any remote calls) are by design asynchronous, requests across a network take a variable amount of time so there's no saying how long a user would have to sit with their machine/process/plugin locked up before the request returns. What you should do instead is pop up a loading dialog at the time the request is made then move your code for showing the other pop-up (and closing the loading dialog) into the complete handler. Basically move this:

alert.cancel();

navigator.pushView(views.PersonSearchResults, +p.getResp());

into the complete handler and make p a private local variable (if you need to instantiate it in advance or set properties on it).



来源:https://stackoverflow.com/questions/5475748/making-httprequest-and-getting-response-adobe-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!