Blog Park Client (Universal App) Development essay--Data base preparation

Source: Internet
Author: User

Before we get started, let's look at the interfaces that the blog park provides:

Blog: http://wcf.open.cnblogs.com/blog/help
News: Http://wcf.open.cnblogs.com/news/help

Take Blog Park _48 hour reading line as an example, return XML such as (RSS, if you open with IE, will prompt you to subscribe. )。

Most of the API in the blog Park Returns RSS (also provides pagination!). ), if you just do a simple RSS reader, you can use syndicationclient directly, after the Retrievefeedasync will parse the XML into SyndicationFeed.

But for binding convenience and other non-RSS APIs, we decided to customize the entity class and then deserialize it with XmlSerializer, using deserialization to save code that was parsed using XmlDocument or LINQ to XML.

Establishment of the project:

Because we are ready to do the Universal App, here we build a support portable library (portable for Universal apps).

This library is actually the previously called PCL, but only Windows 8.1 and Windows phone8.1 are supported by default.

Entity class

For an API that returns RSS, we first need to define the Feed<t> class, which contains the basic information of the feed, the entries attribute corresponds to all entry nodes in the XML, and is generic because many entry properties are different.

//feed<t> corresponds to the Feed node, the namespace is required, or the node cannot be found[XmlRoot ("Feed", Namespace ="Http://www.w3.org/2005/Atom")]       Public classFeed<t>      {          //The id attribute corresponds to the value of the FEED/ID node[XmlElement ("ID")]           Public stringId {Get;Set; } [XmlElement ("title")]          Public stringTitle {Get;Set; } //XmlSerializer will parse all the entry nodes under the feed into the corresponding entities and then put them into the list[XmlElement ("entry")]          PublicList<t> Entries {Get;Set; } }

With feed<t>, entity classes are defined for different APIs, such as blogger bloggers.

//This root does not need to namespace, because the feed already has, the deserialization time uses the feed<blogger> to be able to[XmlRoot ("entry")]     Public classBlogger {[XmlElement ("ID")]         Public stringId {Get;Set; } [XmlElement ("title")]         Public stringName {Get;Set; } [XmlElement ("Updated")]         Public stringupdatetimestring {Get;Set; } [XmlElement ("Link",typeof(Link))]  PublicLink Link {Get;Set; } [XmlElement ("Blogapp")]         Public stringBlogapp {Get;Set; } [XmlElement ("Avatar")]         Public stringAvatar {Get;Set; } [XmlElement ("Postcount")]         Public stringPostcount {Get;Set; } [XmlIgnore] PublicDateTime UpdateTime {Get{returnFunctions.parsedatetime ( This. updatetimestring); }        }    }

For news and blogs, most of the properties are the same, so we define a entrybase base class (the most headache to name anything). ), the other can refer to our source code on GitHub, the address at the end of the article.

[XmlRoot (ElementName ="entry")]     Public classentrybase {[XmlElement ("ID")]         Public stringID {Get;Set; } [XmlElement ("title")]         Public stringTitle {Get;Set; } [XmlElement ("Summary")]         Public stringSummary {Get;Set; } [XmlElement ("published")]         Public stringpublishtimestring {Get;Set; } [XmlElement ("Link",typeof(Link))]  PublicLink Link {Get;Set; } [XmlElement ("Blogapp")]         Public stringBlogapp {Get;Set; } [XmlElement ("Diggs")]         Public stringDiggscount {Get;Set; } [XmlElement (" views")]         Public stringViewscount {Get;Set; } [XmlElement ("Comments")]         Public stringCommentscount {Get;Set; } [XmlIgnore] PublicPoststatus status{Get;Set; } [XmlIgnore] PublicDateTime Publishtime {Get            {                returnFunctions.parsedatetime ( This.            publishtimestring); }        }    }

One of the updatetime to note is that the DateTime string returned by some API in the blog Park will go wrong when deserializing (it should be less time zone or something), so here is a string (lazy here). ), and then use within the application when you parse it yourself.

Non-RSS news content:

XmlElementAttribute the node name is not written, the node name and the attribute name are the same (including case), as in the following XML.

<?XML version= "1.0" encoding= "Utf-8"?><Newsbodyxmlns:xsd= "Http://www.w3.org/2001/XMLSchema"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"><Title>Sun Yu Entrepreneur: I measure people's standards is how much money you make</Title><SourceName>Investment community</SourceName><submitdate>2014-12-15 16:30:59</submitdate><Content>I now measure the standards of others, this man-made society ...</Content><IMAGEURL>Http://images.cnitblog.com/news/66372/201412/151630299226167.jpg</IMAGEURL><prevnews>510939</prevnews><nextnews/><Commentcount>0</Commentcount></Newsbody>
public class Newsbody    {        [XmlElement] public        string submitdatestring {get; set;}        [XmlElement]        public string Content {get; set;}        [XmlElement]        public string ImageUrl {get; set;}        [XmlElement]        public string Prevnews {get; set;}        [XmlElement]        public string Nextnews {get; set;}        [XmlElement]        public string Commentcount {get; set;}        [XmlElement]        public string Title {get; set;}        [XmlIgnore]        Public DateTime submitdate        {            get {                return Functions.parsedatetime (this. submitdatestring);}}}    
 Public classnewsbody {[XmlElement] Public stringsubmitdatestring {Get;Set; } [XmlElement] Public stringContent {Get;Set; } [XmlElement] Public stringIMAGEURL {Get;Set; } [XmlElement] Public stringprevnews {Get;Set; } [XmlElement] Public stringnextnews {Get;Set; } [XmlElement] Public stringCommentcount {Get;Set; } [XmlElement] Public stringTitle {Get;Set; } [XmlIgnore] PublicDateTime submitdate {Get {                returnFunctions.parsedatetime ( This.            submitdatestring); }        }    }
HTTP Data Request

There are at least 3 classes in WINRT that can be used to implement HTTP data requests: WebRequest, Windows.Web.Http.HttpClient and System.Net.Http.HttpClient and System.Net.Http.HttpClient.

It is recommended to use Windows.Web.Http.HttpClient, the new class, another httpclient may be cut off in a later version, MSDN for this has a special reminder, as for WebRequest to use it up without new httpclient convenience.

HttpClient provides common HTTP request methods: Getasync, Deleteasync, Postasync, and Getstringasync, where Getstringasync is quite handy for requesting Xml/json, The process of turning from response.content to string is omitted (but not httpstatuscode, gains. ), use the following:

New HttpClient (); Try {  stringawait client.} Getstringasync (new  Uri ("API URL Here"));} Catch (Exception) {  // The network will throw an exception if it is not available }

Data requests are simple, and the rest is to stitch the API URLs according to different needs.

PS: When the network is not available, the request will throw System.Exception, with the error code, remember to catch this exception.

Summary

Using httpclient to get XML data is very simple, and with XmlSerializer you can simplify the mapping process from XML to entity classes. We welcome your continued attention.

Windows Phone Store App Link:

http://www.windowsphone.com/zh-cn/store/app/Blog Park-UAP/500F08F0-5BE8-4723-AFF9-A397BEEE52FC

Windows Store App Link:

http://apps.microsoft.com/windows/zh-cn/app/c76b99a0-9abd-4a4e-86f0-b29bfcc51059

GitHub Open Source Link:

Https://github.com/MS-UAP/cnblogs-UAP

MSDN Sample Code:

Https://code.msdn.microsoft.com/CNBlogs-Client-Universal-9c9692d1

Blog Park Client (Universal App) Development essay--Data base preparation

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.