类别:.Net相关知识 / 日期:2015-01-06 / 浏览:2202 / 评论:0

经过一段时间的工作和使用,因为开发的客户端要跟服务器中提供的Web API进行频繁的数据交换,所有将与API调用的部分抽象出了几个方法,今天跟大家一起方法,当然方法不限于此,只是为大家做一个引导。 1、通过GET方式有参获取对象

        /// <summary>
        /// 通过GET方式获取对象
        /// </summary>
        /// <typeparam name="TInput">输入类型</typeparam>
        /// <typeparam name="TOutput">输出类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="msg">提示消息</param>
        /// <param name="prm">参数</param>
        /// <param name="hasProcessDialog">是否需要Loading</param>
        /// <param name="activity">窗体</param>
        /// <returns></returns>
        public static async Task<TOutput> GetObjectByGet<TInput, TOutput>(string url, string msg, TInput prm, bool hasProcessDialog, Activity activity) where TOutput : class
        {
            var dialog = new ProgressDialogHelper();
            if (hasProcessDialog)
            {
                dialog.ShowProgressDialog(activity, ProgressDialogStyle.Spinner, msg);
            }
            var urlNew = url + "?" + QuerystringConvert.ConvertFromObject(prm);
            var client = new HttpClient();
            try
            {
                var httpResponse = await client.GetAsync(urlNew);
                var result = httpResponse.Content;
                var txtStream = await result.ReadAsStreamAsync();
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return JsonConvert.DeserializingObject<TOutput>(txtStream);
            }
            catch (Exception ex)
            {
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return null;
            }
        }

2、通过GET方法无参获取对象

        /// <summary>
        /// 通过GET方法无参数获取对象
        /// </summary>
        /// <typeparam name="TOutput">输出类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="msg">提示消息</param>
        /// <param name="hasProcessDialog">是否需要Loading</param>
        /// <param name="activity">窗体</param>
        /// <returns></returns>
        public static async Task<TOutput> GetObjectByGetWithNoParameter<TOutput>(string url, string msg, bool hasProcessDialog, Activity activity) where TOutput : class
        {
            var dialog = new ProgressDialogHelper();
            if (hasProcessDialog)
            {
                dialog.ShowProgressDialog(activity, ProgressDialogStyle.Spinner, msg);
            }
            var client = new HttpClient();
            try
            {
                var httpResponse = await client.GetAsync(url);
                var result = httpResponse.Content;
                var txtStream = await result.ReadAsStreamAsync();
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return JsonConvert.DeserializingObject<TOutput>(txtStream);
            }
            catch (Exception ex)
            {
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return null;
            }
        }

3、通过POST方式有参获取对象

        /// <summary>
        /// 通过POST方式获取对象
        /// </summary>
        /// <typeparam name="TInput">输入类型</typeparam>
        /// <typeparam name="TOutput">输出类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="msg">提示消息</param>
        /// <param name="prm">参数</param>
        /// <param name="hasProcessDialog">是否需要Loading</param>
        /// <param name="activity">窗体</param>
        /// <returns></returns>
        public static async Task<TOutput> GetObjectByPost<TInput, TOutput>(string url, string msg, TInput prm, bool hasProcessDialog, Activity activity) where TOutput : class
        {
            var dialog = new ProgressDialogHelper();
            if (hasProcessDialog)
            {
                dialog.ShowProgressDialog(activity, ProgressDialogStyle.Spinner, msg);
            }
            var client = new HttpClient();
            try
            {
                var jsonString = JsonConvert.SerializeObject(prm);
                if (jsonString == null)
                {
                    return null;
                }
                var httpResponse = await client.PostAsync(url, new StringContent(jsonString, Encoding.UTF8, "application/json"));
                var result = httpResponse.Content;
                var txtStream = await result.ReadAsStreamAsync();
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return JsonConvert.DeserializingObject<TOutput>(txtStream);
            }
            catch (Exception ex)
            {
                if (hasProcessDialog)
                {
                    dialog.CloseProgressDialog();
                }
                return null;
            }
        }

4、通过GET方式获取字符串

        /// <summary>
        /// 通过GET方式获取字符串
        /// </summary>
        /// <typeparam name="TInput">输入类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="msg">提示信息</param>
        /// <param name="prm">参数</param>
        /// <param name="activity">窗体</param>
        /// <returns></returns>
        public static async Task<string> GetStringByGet<TInput>(string url, string msg, TInput prm, Activity activity)
        {
            var dialog = new ProgressDialogHelper();
            dialog.ShowProgressDialog(activity, ProgressDialogStyle.Spinner, msg);
            var urlNew = url + QuerystringConvert.ConvertFromObject(prm);
            var client = new HttpClient();
            try
            {
                var httpResponse = await client.GetAsync(urlNew);
                var result = httpResponse.Content;
                dialog.CloseProgressDialog();
                var txt = await result.ReadAsStringAsync();
                return txt;
            }
            catch (Exception ex)
            {
                dialog.CloseProgressDialog();
                return null;
            }
        }

5、通过POST方式取字符串

        /// <summary>
        /// 通过POST方式取字符串
        /// </summary>
        /// <typeparam name="TInput">输入类型</typeparam>
        /// <param name="url">请求地址</param>
        /// <param name="msg">提示信息</param>
        /// <param name="prm">参数</param>
        /// <param name="activity">窗体</param>
        /// <returns></returns>
        public static async Task<string> GetStringByPost<TInput>(string url, string msg, TInput prm, Activity activity)
        {
            var dialog = new ProgressDialogHelper();
            dialog.ShowProgressDialog(activity, ProgressDialogStyle.Spinner, msg);
            var client = new HttpClient();
            try
            {
                var jsonString = JsonConvert.SerializeObject(prm);
                if (jsonString == null)
                {
                    return null;
                }
                var httpResponse = await client.PostAsync(url, new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"));
                var result = httpResponse.Content;
                dialog.CloseProgressDialog();
                var txt = await result.ReadAsStringAsync();
                return txt;
            }
            catch (Exception ex)
            {
                dialog.CloseProgressDialog();
                return null;
            }
        }

 

可能感兴趣的文章

评论区

发表评论 /

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。