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

  

posted @ 2020-03-21 10:26  程序&#128049;  阅读(370)  评论(0编辑  收藏  举报