首页 > PHP资讯 > HTML5培训技术 > monotouch通过引用System.Json.dll处理url请求,转成json字符串

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

HTML5培训技术
#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 + "rn");
return null;
}
}

#endregion


具体代码调用:

/// <summary>
/// 获取资讯列表数据
/// </summary>
/// <returns>The items.</returns>
public static List 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;
}
}

HTML5培训技术

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持19不支持0