Ejemplo n.º 1
0
        public async void Send(string _apiUrl, string jsonData, Action <string> _call, string customHost = null, Action pFinishedNoSuccess = null)
        {
            if (WebMessageHelper.IsNeedShowJuhua(_apiUrl))
            {
                UIComponent.Instance.Prompt();
            }
            string url = customHost == null ? $"{GlobalData.Instance.WebURL}{_apiUrl}" : $"{customHost}{_apiUrl}";

            Log.Debug($"send-> url = {url}");

            requests.Add(GameUtil.HttpsPost(url, jsonData, (request, response) =>
            {
                RemoveRequest(request);
                switch (request.State)
                {
                case HTTPRequestStates.Finished:
                    if (response.IsSuccess)
                    {
                        Log.Debug($"response-> url = {url} jsonData = {jsonData} 结果= {response.DataAsText}");
                        CommonResponseData data = JsonHelper.FromJson <CommonResponseData>(response.DataAsText);
                        if (data.status == 105)
                        {
                            //维护弹窗
                            UIComponent.Instance.ShowNoAnimation(UIType.UIDialog,
                                                                 new UIDialogComponent.DialogData()
                            {
                                type          = UIDialogComponent.DialogData.DialogType.Commit,
                                title         = "",
                                content       = string.Format(LanguageMgr.mInstance.GetLanguageForKey("ServerMaintenance"), data.msg),
                                contentCommit = CPErrorCode.LanguageDescription(10008),
                                actionCommit  = null,
                                actionCancel  = null
                            });
                        }
                        if (_call != null)
                        {
                            _call(response.DataAsText);
                        }
                    }
                    else
                    {
                        UIComponent.Instance.Toast(response.StatusCode);
                        if (pFinishedNoSuccess != null)
                        {    //追加的.下拉列表 不成功时 收缩下(加载中...)
                            pFinishedNoSuccess();
                        }
                    }
                    break;

                case HTTPRequestStates.Error:
                    string exception = request.Exception != null
                                ? (request.Exception.Message + "\n" + request.Exception.StackTrace) : "No Exception";
                    Log.Debug($"Request Finished with Error!  {exception}");
                    // UIComponent.Instance.Toast("请求错误");
                    if (pFinishedNoSuccess != null)
                    {
                        pFinishedNoSuccess();
                    }
                    break;

                case HTTPRequestStates.Aborted:
                    Log.Debug($"Request Aborted!");
                    // UIComponent.Instance.Toast("请求拒绝");
                    if (pFinishedNoSuccess != null)
                    {
                        pFinishedNoSuccess();
                    }
                    break;

                case HTTPRequestStates.ConnectionTimedOut:
                    Log.Debug($"Connection Timed Out!");
                    // UIComponent.Instance.Toast("连接超时");
                    if (pFinishedNoSuccess != null)
                    {
                        pFinishedNoSuccess();
                    }
                    break;

                case HTTPRequestStates.TimedOut:
                    Log.Debug($"Processing the request Timed Out!");
                    // UIComponent.Instance.Toast("网络超时");
                    if (pFinishedNoSuccess != null)
                    {
                        pFinishedNoSuccess();
                    }
                    break;

                default:
                    UIComponent.Instance.Toast($"未知错误 State = {request.State} ");
                    if (pFinishedNoSuccess != null)
                    {
                        pFinishedNoSuccess();
                    }
                    break;
                }
            }), (int)HTTPRequestStates.Initial);
        }