我喜欢制作一个应用程序来读取网站广告 RSS 或 JSON 中的文章。我使用什么方法将 XML 或 JSON 抓取到我的应用程序中?如何判断数据何时在本地可用?
谢谢。
编辑: 我正在为 iOS 使用 Objective-c。我需要 iOS 代码。
最佳答案
我不知道你使用什么编程语言,但我可以分享我用 C# 开发的代码片段。它获取 wordpress 博客的 rss 内容,解析该内容,并显示 3 个顶级博客文章链接。
int blogitemcount = 3;
string wordpressBlog = "wordpress blog address";
System.Net.WebClient wc = new System.Net.WebClient();
byte[] buffer = wc.DownloadData("http://" + wordpressBlog + "/feed/rss");
wc.Dispose();
wc = null;
if(buffer.Length > 0) {
string content = Encoding.UTF8.GetString(buffer);
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(content);
XmlNodeList nodes=xdoc.SelectNodes("/rss/channel/item");
string linkformat = "<a href='{0}' target='_blank' class='blogitem'>{1}</a>";
for(int i=0; i < nodes.Count && i < blogitemcount;i++ )
{
XmlNode n = nodes[i];
this.ltItems.Text += string.Format(linkformat, n.SelectSingleNode("link").InnerText, n.SelectSingleNode("title").InnerText);
}
}
关于xml - 为 iOS 开发 RSS 阅读器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3596907/