flex mobile project: Establish http connection to a specific url
how to establish http connectio开发者_如何学编程n to a specific url and get data from that url in flex mobile project.
Thanks
The function:
/* Function to make a generic HTTP Request
* loc = location e.g. http://www.google.com
* req = request
* type = content type
* complete = function to execute when HTTP req returns with complete
*/
public function httpReq(loc:String , req:String, type:String, complete:Function):void{
/* Start the HTTP Request */
try{
requestSender= new URLLoader();
requestSender.addEventListener(Event.COMPLETE, complete);
requestSender.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, printResponse);
var urlRequest :URLRequest = new URLRequest(loc);
/* Setup HTTP Request */
urlRequest.data = req;
urlRequest.contentType = type;
urlRequest.method = URLRequestMethod.GET;
requestSender.load(urlRequest); // Send the request off, when complete jump to completeHandler
}
catch(e:Error){
trace(e.message);
trace(e.getStackTrace());
}
}
Get the response:
/* HTTP Request complete, back with response */
private function responseHandler(event:Event):void
{
this.requestSender = URLLoader(event.target); // Set requestSender as response
this.output.text = this.requestSender.data; // Get its data
trace("THE DATA IS "+this.requestSender.data);
this.requestSender.close();
alert.cancel();
}
Example call:
httpReq("http://www.google.com" , "requestString", "application/x-www-form-urlencoded", responseHandler);
Did you try doing any research?
http://help.adobe.com/en_US/flex/mobileapps/index.html
http://help.adobe.com/en_US/flex/mobileapps/developing_mobile_apps_flex.pdf
http://help.adobe.com/en_US/flex/accessingdata/WS2db454920e96a9e51e63e3d11c0bf69084-7ff2.html
精彩评论