egret http协议封装使用
http协议:
创建 HttpHandler类
class HttpHandler { constructor() { } public static sendMsgCallBack(URL:string, param:string, callback:Function, method:string, thisObj:any):void { var loader:egret.URLLoader = new egret.URLLoader(); loader.dataFormat = egret.URLLoaderDataFormat.TEXT; var request:egret.URLRequest = new egret.URLRequest(URL); request.method = method; request.data = new egret.URLVariables(param); loader.load(request); loader.addEventListener(egret.Event.COMPLETE, function(event:egret.Event) { var loader:egret.URLLoader = <egret.URLLoader>event.target; var data:egret.URLVariables = loader.data; Global.consoleLog(data.toString()); var obj:any = JSON.parse(data.toString()); if(thisObj != null) callback.call(thisObj, obj); }, this); Global.consoleLog("URL: " + URL + " param:" + param) } }
调用HttpHandler.sendMsgCallBack
HttpHandler.sendMsgCallBack("https://test.baidu.cn/api.php", "param=" + JSON.stringify(param), this.recordClientListCallback, egret.URLRequestMethod.POST, this);
响应调用函数
private recordClientListCallback(obj: any): void { if (obj.error_code == 0) { 成功 } else { EffectUtils.showTips("用户不存在", 5, false); } }
山重水复疑无路,柳暗花明又一村!
专注填坑,少走弯路!