iOS 通过URL网络获取XML数据的两种方式
转载于:http://blog.csdn.net/crayondeng/article/details/8738768
下面简单介绍如何通过url获取xml的两种方式。
第一种方式相对简单,使用NSData的构造函数dataWithContentsOfURL;不多解释,直接上代码咯。
- NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"];
- //A Boolean value that turns an indicator of network activity on or off.
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- NSData *xmlData = [NSData dataWithContentsOfURL:url];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
- if (xmlData == nil) {
- NSLog(@"File read failed!:%@", xmlString);
- }
- else {
- NSLog(@"File read succeed!:%@",xmlString);
- }
上面的 NSData *xmlData = [NSData dataWithContentsOfURL:url]; 就是获取xml的关键啦。
注意:如果直接输出xmlData的话会是一对unicode,所以用UTF-8编码转换成是String进行输出就好啦。
第二种方式:通过NSURLConnection建立网络连接,并实现它的几个委托方法,从而获取数据。
代码如下,应该可以看懂的。
- @implementation ViewController {
- BOOL getXmlDone; //是否停止获取xml数据的标志
- NSMutableData *xmlData; //存储获得的xml数据
- }
- //自定义的一个方法
- - (void) getXML {
- //获取xml
- xmlData = [NSMutableData data];
- //Clears the receiver’s cache, removing all stored cached URL responses.
- //清除接收器的缓存,删除所有存储的高速缓存的URL的响应。
- [[NSURLCache sharedURLCache] removeAllCachedResponses];
- NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"];
- NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
- //create the connection with the request and start loading the data
- NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- [self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO];
- if (urlConnection != nil) {
- do {
- [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- } while (!getXmlDone);
- }
- }
- #pragma mark NSURLConnection Delegate methods
- - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
- return nil;
- }
- // Forward errors to the delegate.
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- getXmlDone = YES;
- }
- // Called when a chunk of data has been downloaded.
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- // Append the downloaded chunk of data.
- [xmlData appendData:data];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- [self performSelectorOnMainThread:@selector(downloadEnded) withObject:nil waitUntilDone:NO];
- getXmlDone = YES;
- }
- - (void)downloadStarted {
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- }
- - (void)downloadEnded {
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- [self getXML];
- NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
- NSLog(@"data: %@",xmlString);
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
小结一下,第一种简单,一次性获取所有的xml数据,第二种有点复杂,当然其灵活性也更好。
要验证获取的数据的准确性的话,就点击你的url吧!http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction
OK,下一篇文章将会介绍如何解析xml。come on!
iOS 通过URL网络获取XML数据的两种方式的更多相关文章
- SparkStreaming获取kafka数据的两种方式:Receiver与Direct
简介: Spark-Streaming获取kafka数据的两种方式-Receiver与Direct的方式,可以简单理解成: Receiver方式是通过zookeeper来连接kafka队列, Dire ...
- 工具篇-Spark-Streaming获取kafka数据的两种方式(转载)
转载自:https://blog.csdn.net/weixin_41615494/article/details/7952173 一.基于Receiver的方式 原理 Receiver从Kafka中 ...
- Spark-Streaming获取kafka数据的两种方式:Receiver与Direct的方式
简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 Receiver 使用Kafka的高层次Consumer API来 ...
- spark-streaming获取kafka数据的两种方式
简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 一.Receiver方式: 使用kafka的高层次Consumer ...
- jQuery异步获取json数据的2种方式
jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: ...
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...
- easyUI之datagrid绑定后端返回数据的两种方式
先来看一下某一位大佬留下的easyUI的API对datagrid绑定数据的两种方式的介绍. 虽然精简,但是,很具有“师傅领进门,修行靠个人”的精神,先发自内心的赞一个. 但是,很多人和小编一样,第一次 ...
- SparkStreaming与Kafka,SparkStreaming接收Kafka数据的两种方式
SparkStreaming接收Kafka数据的两种方式 SparkStreaming接收数据原理 一.SparkStreaming + Kafka Receiver模式 二.SparkStreami ...
- strus2中获取表单数据 两种方式 属性驱动 和模型驱动
strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...
随机推荐
- 中兴u880e精简教程
精简软件请参考此处 (A代表可以删除,B代表建议别删除.)删或留你做主. Accounts AndSyncSettings.apk 账户与同步设置 A alarming.apk 闹钟时钟 A Appl ...
- Go安装一些第三方库
原文链接:https://javasgl.github.io/go-get-golang-x-packages/ 侵权联系删除! go在go get 一些 package时候的会由于众所周知的原因而无 ...
- Leetcode 1022. Sum of Root To Leaf Binary Numbers
dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...
- checkbox不显示,试试去掉-webkit-appearance这个样式
目前在项目中发现一个大坑,搞的我找了好久不知道因为什么,自用的reset.css中加入了 -webkit-appearance: none;其他所有表单没有出现问题,但checkbox会不显示,值改为 ...
- python try except, 异常处理
http://www.runoob.com/python/python-exceptions.html http://blog.sciencenet.cn/blog-3031432-1059523.h ...
- yum安装apache及问题解决
一.检查服务器上是否已经安装了apache apache在linux系统里的名字是httpd,执行以下命令,如果有返回的信息,则会显示已经安装的软件.如果没有则不会显示其它的信息. rpm -qa h ...
- 隐藏 FastAdmin 列表中的拖动排序按钮
隐藏 FastAdmin 列表中的拖动排序按钮 就是以下这个按钮,想先删除不要. 刚开始在 CMS 插件中的栏目中发现没有,以为在哪个位置中,但找到半天的 weigh 都没有找到weigh 的字眼. ...
- Phonegap 自定义插件
一.PhoneGap中js与Java之间相互调用分为同步和异步两种方式 1.同步:js调用Java类的方法,然后Java类的方法直接返回一个值给js端 2.异步:js调用Java类的方法,Java类的 ...
- jquery 绘图工具 flot 学习笔记
今天想做一个统计图表,像163博客的流量统计一样的,借助 flot 实现了,而且很简单. flot网址:http://code.google.com/p/flot/ 下载 JS 文件,使用方法和 jq ...
- ASI和TS流有什么区别
TS流是信源码流,最高码率为44.209 Mbit/s,它是经过信源编码后的压缩码流,为了使欲传输的信源信息在传输速率一定的条件下更快更多地传输,还要把数据进行压缩,也就是通过信源编码去掉信息中多余的 ...