Monotouch processes url requests by referencing System. Json. dll and converts them to json strings.

Source: Internet
Author: User
Regionhttp request, get the JsonObject object, get the JsonObject object, TheresultsjsonRequestURL # region http request, get the JsonObject object
/// < Summary >
/// Get 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 ()){ // If it is offline, null is returned directly.
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


Code call:

/// <Summary>
///Obtain information list data
/// </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 ("failed to get data! ");
Return null;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.