如何从 Web 服务器上的 xml 文件读取数据
我想从放置在 Web 服务器上的 xml 文件读取数据。然后我想在 UITableView 中显示这些数据。有人可以帮助我如何从文件中读取数据。
另外我需要每 30 秒更新一次数据。所以有人可以告诉我如何每 30 秒读取该 xml 文件。
提前致谢。
问候, 高拉夫·阿罗拉
I want to read data from a xml file placed on a web server. then I want to display this data in a UITableView. Can someone help me how can I read data from a file.
Also I need to update the data in each 30 sec.So can someone tell me how can I read that xml file in every 30 secs.
Thanks in advance.
Regards,
Gaurav Arora
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
NSUrlConnection
从服务器获取 XML 文档。我个人认为,使用 NSUrlConnection 的漂亮包装来简化应用程序代码会更方便。目前我在我的项目中使用 ASIHHTPRequest 和 GTM-HTTP-Fetcher。两个图书馆对我来说都运行得很好。
收到 XML 文档后,您应该创建一个 NSXMLParser 来提取信息。请注意,基于文档的 API 在 iPhone 上不可用。
从服务器收到信息后,您可能会考虑使用
dispatch_after
重新安排一个新的执行块,并延迟 30 秒以激活重新获取。我建议在后台操作中执行远程服务器访问和 XML 解析,这样不会阻塞主循环管理的 UI 线程。一旦在后台收到新数据,我通常会使用
dispatch_async()
将执行块放入主 (UI) 队列中,该队列处理UITableView
上的更新>。You might use
NSUrlConnection
to fetch the XML document from the server. Personally I think, it is more convenient to use a nice wrapper aroundNSUrlConnection
to simplify your application code.At the moment I use ASIHHTPRequest and GTM-HTTP-Fetcher in my projects. Both libraries worked quiet well for me.
After receiving the XML document, you should create an
NSXMLParser
to ingest the information. Please remind that the document based API its not available on the iPhone.After information has been received from the server, you might think about rescheduling an new execution block using
dispatch_after
with a delay of 30 seconds to activate refetching.I recommend to execute the remote server access and XML parsing in an background operation that does not block the UI thread managed by the main loop. As soon as new data has been received in the background, i normally use
dispatch_async()
to put an execution block onto the main (UI) queue that processes the update on theUITableView
.