コード例 #1
0
        /// <summary>
        /// Sends the write board.
        /// 掲示板書き込み時にかえってくるのは成功かどうかだけなので
        /// </summary>
        /// <returns>The write board.</returns>
        /// <param name="url">URL.</param>
        /// <param name="reqPostDatas">Req post datas.</param>
        /// <param name="reqPostBinaryDatas">Req post binary datas.</param>
        /// <param name="callback">Callback.</param>
        public static string SendWriteBoard(string url, Dictionary <string, string> reqPostDatas, Dictionary <string, Texture2D> reqPostBinaryDatas, Action <string> callback)
        {
            _reqSuccess = false;

            var request = new HTTPRequest(new Uri(url), HTTPMethods.Post, (req, resp) =>
            {
                switch (req.State)
                {
                // The request finished without any problem.
                case HTTPRequestStates.Finished:
                    if (resp.DataAsText.Contains("error") == true)
                    {
                        var err = StringLib.JsonToObject <ErrorEntity.Result> (resp.DataAsText);
                        //サーバー系のエラーキャッチ
                        if (err.result.error != null)
                        {
                            PopUpWindow(err.result.error [0]);
                            break;
                        }
                    }

                    callback(resp.DataAsText);
                    _reqSuccess = true;
                    break;

                // The request finished with an unexpected error.
                // The request's Exception property may contain more information about the error.
                case HTTPRequestStates.Error:
                    Debug.LogError("Request Finished with Error! " +
                                   (req.Exception != null ?
                                    (req.Exception.Message + "\n" +
                                     req.Exception.StackTrace) :
                                    "No Exception"));
                    break;

                // The request aborted, initiated by the user.
                case HTTPRequestStates.Aborted:
                    Debug.LogWarning("Request Aborted!"); break;

                // Ceonnecting to the server timed out.
                case HTTPRequestStates.ConnectionTimedOut:
                    Debug.LogError("Connection Timed Out!"); break;

                // The request didn't finished in the given time.
                case HTTPRequestStates.TimedOut:
                    Debug.LogError("Processing the request Timed Out!"); break;
                }
            });

            if (reqPostDatas.Count > 0)
            {
                foreach (var param in reqPostDatas)
                {
                    request.AddField(param.Key, param.Value);
                }
            }

            if (reqPostBinaryDatas.Count > 0)
            {
                foreach (var param in reqPostBinaryDatas)
                {
                    Texture2D expectedImage = param.Value;
                    byte[]    binaryData    = expectedImage.EncodeToJPG();

                    request.AddBinaryData(param.Key, binaryData, param.Key, "image/jpeg");
                }
            }

            request.ConnectTimeout = TimeSpan.FromMilliseconds(_connectTimeout);
            request.Timeout        = TimeSpan.FromSeconds(_reqTimeOut);
            request.DisableCache   = true;
            request.Send();
            return("");
        }
コード例 #2
0
        /// <summary>
        /// Send the specified url, reqPostDatas, callback and errorCallBack.
        /// 通常のリクエスト用。
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="reqPostDatas">Req post datas.</param>
        /// <param name="callback">Callback.</param>
        /// <param name="errorCallBack">Error call back.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static T Send <T> (string url, Dictionary <string, string> reqPostDatas, Action <T> callback, Action <Http.ErrorEntity.Error> errorCallBack = null)
        {
            // ネットワークの状況により処理を変更
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                //ネットワークに接続不可な場合の処理
                PopUpWindow(LocalMsgConst.NETWORK_ERROR_TEXT);
                return(default(T));
            }

            _reqSuccess = false;
            HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, (req, resp) =>
            {
                switch (req.State)
                {
                // The request finished without any problem.
                case HTTPRequestStates.Finished:
                    if (resp.DataAsText.Contains("error") == true)
                    {
                        var err = StringLib.JsonToObject <ErrorEntity.Result> (resp.DataAsText);
                        //サーバー系のエラーキャッチ
                        if (err.result.error != null)
                        {
                            PopUpWindow(err.result.error[0]);

                            if (errorCallBack != null)
                            {
                                errorCallBack(err.result);
                            }
                            break;
                        }
                    }

                    Debug.Log("Request Finished Successfully!\n" + resp.DataAsText);

                    //if (err.result.error != null) {
                    //    Debug.Log (err.result.error);
                    //    break;
                    //}

                    callback(StringLib.JsonToObject <T>(resp.DataAsText));

                    _reqSuccess = true;
                    break;

                // The request finished with an unexpected error.
                // The request's Exception property may contain more information about the error.
                case HTTPRequestStates.Error:
                    Debug.LogError("Request Finished with Error! " +
                                   (req.Exception != null ?
                                    (req.Exception.Message + "\n" +
                                     req.Exception.StackTrace) :
                                    "No Exception"));
                    break;

                // The request aborted, initiated by the user.
                case HTTPRequestStates.Aborted:
                    Debug.LogWarning("Request Aborted!"); break;

                // Ceonnecting to the server timed out.
                case HTTPRequestStates.ConnectionTimedOut:
                    Debug.LogError("Connection Timed Out!"); break;

                // The request didn't finished in the given time.
                case HTTPRequestStates.TimedOut:
                    Debug.LogError("Processing the request Timed Out!"); break;
                }
            });

            //HTTPMethods.Post
            // Very little time, for testing purposes:
            Debug.Log(url);

            if (reqPostDatas.Count > 0)
            {
                foreach (var param in reqPostDatas)
                {
                    Debug.Log("key: " + param.Key + " val: " + param.Value);

                    request.AddField(param.Key, param.Value);
                }
            }
            request.ConnectTimeout = TimeSpan.FromMilliseconds(_connectTimeout);
            request.Timeout        = TimeSpan.FromSeconds(_reqTimeOut);
            request.DisableCache   = true;
            request.Send();
            return(default(T));
        }