mono touch通过引用System.Json.dll处理url请求,转成json字符串

有不明白的地方欢迎入群 347636249 探讨

#region http请求,获取JsonObject对象

        /// <summary>
        /// 获取JsonObject对象
        /// </summary>
        /// <returns>The results json.</returns>
        /// <param name="requestUrl">Request URL.</param>
        public static JsonObject ParseResultsJson (object requestUrl)
        {
            HttpWebResponse httpRes = GetRequestInfo (requestUrl);
            if (httpRes == null)
                return null;

            using (Stream s = httpRes.GetResponseStream ()) {
                return (JsonObject)JsonObject.Load (s);
            }
        }

        /// <summary>
        /// Get Request Url Information by request method type "get"
        /// </summary>
        /// <param name="requestUrl">Request URL.</param>
        public static HttpWebResponse GetRequestInfo (object requestUrl)
        {
            if (!Utils.Tools.IsConnect ()) { //如果是离线就直接返回空
                return null;
            }

            try {
                HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create (new Uri (requestUrl.ToString ()));
                httpReq.ContentType = "application/json";
                httpReq.Method = "GET";
                using (HttpWebResponse httpRes=(HttpWebResponse)httpReq.GetResponse()) {
                    return httpRes;
                }
            } catch (Exception e) {
                Utils.Tools.AppendToLog ("xxxxx.MyTestVC.GetRequestInfo()", e.Message + "\r\n");
                return null;
            }
        }

        #endregion


具体代码调用:

/// <summary>
        /// 获取资讯列表数据
        /// </summary>
        /// <returns>The items.</returns>
        public static List<ArticleModel> GetArticleList(bool isrefresh = false)
        {
            if (isrefresh)
                NSUserDefaults.StandardUserDefaults.RemoveObject (KeyCenter.Key_NewsList);

            string _url = string.Format (Utils.UrlCenter.UrlNewsList, "10", HttpUtility.UrlDecode ("前沿资讯"));
            JsonObject j CacheHelp.ParseResultsJson (_url);
            if (j != null) {
                var results = (from t in (JsonArray)j ["articleInfoList"]
                           let r = t as JsonObject
                           select new ArticleModel {
                    Articleid=t["Articleid"],
                    Articletitle = t["Articletitle"],
                    ArticleContent =t["articleContent"],
                    Articlechannel = t["Articlechannel"],
                    Articlecreatedate=t["Articlecreatedate"],
                    Articleimg=t["Articleimg"],
                    ArticleAuthor=t["ArticleAuthor"],
                    Outline=t["outline"],
                    ClickNumber = t["clicknumber"]
                }).ToList ();
                return results;
            } else {
                Utils.Tools.Alert ("获取数据失败!");
                return null;
            }
        }

你可能感兴趣的:(json,HttpWebRequest,HttpWebResponse)