Ejemplo n.º 1
0
        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="httpInvoke"></param>
        /// <param name="processError">异常处理,会抛出异常</param>
        /// <param name="processSuccess">成功处理</param>
        /// <param name="loadArgs">Http基础请求添加额外字段</param>
        /// <returns></returns>
        public static T Request <T>(this HttpInvoke httpInvoke,
                                    Action <Result> processError,
                                    Action <T> processSuccess = null,
                                    Func <Dictionary <string, string> > loadArgs = null) where T : class
        {
            var result = Run(httpInvoke, loadArgs);

            if (result.Wrong == Wrong.UnLogin || result.Wrong == Wrong.LoginTimeOut)
            {
                var token = GetLogin().Login(httpInvoke.AppID);
                SetToken(token);
            }
            if (result.IsSuccess)
            {
                var t = _jsonConvert.DeserializeObject <T>(result.Data);
                if (t != null)
                {
                    processSuccess?.Invoke(t);
                }
                else
                {
                    result.IsSuccess = false;
                    result.Wrong     = Wrong.JsonDeserializeNullError;
                    processError(result);
                }
                return(t);
            }
            else
            {
                processError(result);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserialize object T from json string.
        /// </summary>
        /// <returns>default(T) if failed.</returns>
        public T DeserializeObject <T>(string jsonString) where T : class
        {
            T value = default(T);

            if (!string.IsNullOrEmpty(jsonString))
            {
                try { value = JsonConvert.DeserializeObject <T>(jsonString); }
                catch { }
            }

            return(value);
        }