iOS 通过URL网络获取XML数据的两种方式
2014-04-21 11:29
501 查看
转载于:http://blog.csdn.net/crayondeng/article/details/8738768
下面简单介绍如何通过url获取xml的两种方式。
第一种方式相对简单,使用NSData的构造函数dataWithContentsOfURL;不多解释,直接上代码咯。
[cpp] view
plaincopy
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建立网络连接,并实现它的几个委托方法,从而获取数据。
代码如下,应该可以看懂的。
[cpp] view
plaincopy
@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!
下面简单介绍如何通过url获取xml的两种方式。
第一种方式相对简单,使用NSData的构造函数dataWithContentsOfURL;不多解释,直接上代码咯。
[cpp] view
plaincopy
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建立网络连接,并实现它的几个委托方法,从而获取数据。
代码如下,应该可以看懂的。
[cpp] view
plaincopy
@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数据的两种方式
- iOS 通过URL网络获取XML数据的两种方式
- ios通过URL地址,从网络上获取图片
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- iOS ——XML数据结构及其两种解析数据的方式SAX 、DOM
- ios通过URL获取网络图片的原始大小
- ios通过URL获取网络图片的原始大小
- java通过url获取页面数据 java解析xml 基金净值接口使用方法
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- 通过ashx获取JSON数据的两种方式
- 通过URLHttpConnection方式连接网络步骤,获取位图为例
- iOS 网络请求数据两种请求方式GET,POST
- 在线程中使用HttpClicent获取网络数据(get和post两种方式)
- (转)Android通过URL获取网络数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- android:通过url向网络端获取json格式数据并解析显示
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- 【iOS开发-网络】两种常用的方法解析XML数据
- Android两种方式获取网络数据
- Android通过URL获取网络数据