iOS 通过URL网络获取XML数据的两种方式
来源:互联网 发布:q房网经纪人端口下载 编辑:程序博客网 时间:2024/06/07 12:06
下面简单介绍如何通过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 {//获取xmlxmlData = [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
要验证获取的数据的准确性的话,就点击你的url吧!http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction
OK,下一篇文章将会介绍如何解析xml。come on!
- iOS 通过URL网络获取XML数据的两种方式
- iOS 通过URL网络获取XML数据的两种方式
- 通过ashx获取JSON数据的两种方式
- Android两种方式获取网络数据
- ios通过URL获取网络图片的原始大小
- ios通过URL获取网络图片的原始大小
- Android通过URL获取网络数据
- Android通过URL获取网络数据
- (转)Android通过URL获取网络数据
- Android通过URL获取网络数据
- PHP通过URL传参的两种方式
- ajax获取数据的两种方式
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Javascript获取URL参数的两种方式
- js获取url参数值的两种方式
- powershell connect sql server
- powershell seach const variable
- 通过反射获得类的实例对象
- EditView 输入问题
- Android APK反编译
- iOS 通过URL网络获取XML数据的两种方式
- 【婷】3月29日 小记
- 完成端口(CompletionPort)详解
- 观察者模式——Head First Design Patterns
- Harry Potter’s Exam (an application of Dijkstra algorithm)
- 程序题:Java代码实现1-1/2+1/3-1/4+...+1/100
- Android提交数据到服务器的两种方式四种方法
- java实现多线程的两种方法
- SGU 110 三维计算几何 向量的灵活运用