AS3与后台交互—自己封装的简单类

package utils
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

import ui.EnterGamePage;
import ui.MessagePanel;

public class HttpRequest
{
private var _url:String;
private var _urlParameter:URLVariables;
private var _urlLoader:URLLoader;
private var _result:*;
private var _method:String="POST";
private var _onComplete:Function;

public function HttpRequest(URL:String,Parameter:Object=null)
{
_urlParameter = new URLVariables();
url =  URL;
parameter = Parameter;
_method = URLRequestMethod.GET;
}
public function execute():void{
try{
var request:URLRequest = new URLRequest(_url);
request.method = URLRequestMethod.GET;
request.data = _urlParameter;
_urlLoader= new URLLoader();
_urlLoader.load(request);
_urlLoader.addEventListener(Event.COMPLETE,onExecuteComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onError);
_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
}catch(err:Error){
trace(err.message);
}
}
private function onSecurityError(evt:SecurityErrorEvent):void{
trace("Security error :"+evt.text);
}
private function onExecuteComplete(evt:Event):void{
_urlLoader.removeEventListener(Event.COMPLETE,onExecuteComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
_result = evt.target.data;
if (_onComplete!=null){
_onComplete(_result);
}
}
private function onError(evt:IOErrorEvent):void{
_urlLoader.removeEventListener(Event.COMPLETE,onExecuteComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
trace("server error :"+evt.text);
}
public function set parameter(value:Object):void{
if (value){
for (var item:* in value){
_urlParameter[item] = value[item];
}
}
}
public function set url(value:String):void
{
_url = value;
}
public function set onComplete(value:Function):void{
_onComplete = value;
}
}
}

你可能感兴趣的:(as3)