Blog garden client (Universal App) Development essay, universalapp
Before starting, let's take a look at the interfaces provided by the blog Park:
Blog: http://wcf.open.cnblogs.com/blog/help
News: http://wcf.open.cnblogs.com/news/help
Take the blog garden's 48-hour reading ranking as an example. The returned Xml is like (RSS. If you open it with IE, you will be prompted to subscribe ..).
Most of the APIs in the blog Park return RSS (pages are also provided !), If you just make a simple RSS reader, you can directly use SyndicationClient. After RetrieveFeedAsync, the XML will be parsed into SyndicationFeed.
However, to bind APIs that are convenient and take into account other non-RSS APIs, we decided to customize the object class and then use XmlSerializer for deserialization, deserialization can save the code parsed using XmlDocument or linq to xml.
Project Creation:
Because we are preparing to do the Universal App, here we create a Portable library (Portable for Universal Apps ).
This library is actually the so-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 to include the basic information of the feed. The Entries attribute corresponds to all entry nodes in XML, Which is generic, because many entry attributes are different.
// Feed <T> corresponds to the feed node, the Namespace is required, otherwise the node [XmlRoot ("feed", Namespace = "http://www.w3.org/2005/Atom")] public class Feed <T> {// Id attribute corresponds to the value of the feed/id node [XmlElement ("id")] public string Id {get; set ;} [XmlElement ("title")] public string Title {get; set;} // XmlSerializer parses all entry nodes in the feed into corresponding entities, put the List [XmlElement ("entry")] public List <T> Entries {get; set ;}}
With the Feed <T>, define entity classes for different APIs, such as recommending the Blogger's Blogger.
// This root does not need namespace, because the Feed already exists. You can use Feed <Blogger> to [XmlRoot ("entry")] During deserialization. public class Blogger {[XmlElement ("id")] public string Id {get; set;} [XmlElement ("title")] public string Name {get; set ;} [XmlElement ("updated")] public string UpdateTimeString {get; set;} [XmlElement ("link", typeof (Link)] public Link {get; set ;} [XmlElement ("blogapp")] public string BlogApp {get; set;} [XmlElement ("avatar")] public string Avatar {get; set ;} [XmlElement ("postcount")] public string PostCount {get; set;} [XmlIgnore] public DateTime UpdateTime {get {return Functions. parseDateTime (this. updateTimeString );}}}
For news and blogs, most of the attributes are the same, so we defined an EntryBase base class (the biggest headache for getting a name or something ..), For other information, refer to the source code on github. The address is at the end of the article.
[XmlRoot(ElementName = "entry")] public class EntryBase { [XmlElement("id")] public string ID { get; set; } [XmlElement("title")] public string Title { get; set; } [XmlElement("summary")] public string Summary { get; set; } [XmlElement("published")] public string PublishTimeString { get; set; } [XmlElement("link", typeof(Link))] public Link Link { get; set; } [XmlElement("blogapp")] public string BlogApp { get; set; } [XmlElement("diggs")] public string DiggsCount { get; set; } [XmlElement("views")] public string ViewsCount { get; set; } [XmlElement("comments")] public string CommentsCount { get; set; } [XmlIgnore] public PostStatus Status{ get; set; } [XmlIgnore] public DateTime PublishTime { get { return Functions.ParseDateTime(this.PublishTimeString); } } }
Among them, UpdateTime should be noted that the DateTime string returned by Some APIs in the blog Park will encounter an error during deserialization (it should be because the time zone is missing ), so here we use string directly (here we are lazy ..), Then, you can resolve it yourself when using it in the application.
Non-RSS news content:
If XmlElementAttribute does not write a node name, it indicates that the node name and attribute name are the same (including case sensitivity), as shown in the following Xml section.
<? Xml version = "1.0" encoding = "UTF-8"?> <NewsBody xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"> <Title> post-90 entrepreneurs sun yuchen: my measure is how much you earn </Title> <SourceName> Investment </SourceName> <SubmitDate> 16:30:59 </SubmitDate> <Content> my measure of others' standards, this person is a 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 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); } } }
Http data request
Currently, at least three classes can be used in winrt to implement Http data requests: WebRequest, Windows. web. http. httpClient and System. net. http. httpClient and System. net. http. httpClient.
Windows is recommended. web. http. the newly added HttpClient class, and the other HttpClient may be cut off in a later version. MSDN has a special reminder for this. As for WebRequest, there is no new HttpClient convenience.
HttpClient provides common Http request methods: GetAsync, DeleteAsync, PostAsync, and GetStringAsync. GetStringAsync is convenient for XML/Json requests, saving the need for response. the process of converting content into a string (but it does not get HttpStatusCode ..), Use:
HttpClient client = new HttpClient (); try {string xmlString = await client. getStringAsync (new Uri ("api url here");} catch (Exception) {// Exception thrown when the network is unavailable}
The data request is so simple, and the rest is to splice the api url according to different requirements.
PS: when the network is unavailable, the request will throw System. Exception with the error code. Remember to catch this Exception.
Summary
Using HttpClient to obtain Xml data is very simple. With XmlSerializer, You can simplify the ing process from Xml to object classes. You are welcome to continue your attention.
Windows Phone Store App link:
Bytes
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