开发者

Making HTTPRequest and getting response (Adobe Flex)

开发者 https://www.devze.com 2023-02-20 09:16 出处:网络
Im trying to make a HTTP Request in Adobe Flex (Actionscript) as follows: var p:PersonSearchController = new PersonSearchController();

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");

va开发者_开发技巧r 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


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" />


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消