#import "ViewController.h"
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self start];
NSLog(@"%@",[_notes objectForKey:@"data_1"]);
_labal.text=[NSString stringWithFormat:@"%@%@的天气状况",[_notes objectForKey:@"data_1"], [_notes objectForKey:@"city"]];
}
-(void)start{
// 1. url确定资源
NSURL *url = [NSURL URLWithString:@"http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100"];
// 2. request建立请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 发送异步请求,新建数据处理队列,待数据处理完成后,再更新UI
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 1> 实例化XML解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 2> 设置解析器的代理
//parser.delegate = self;
[parser setDelegate:self];
// 3> 开始解析
[parser parse];
}];
}//文档开始的时候触发
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
_notes=[NSDictionary new];
}//文档出错的时候触发
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%@",parseError);
}//遇到一个开始标签时候触发
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict{
_currentTagName=elementName;
NSLog(@"_currentTagName=%@",_currentTagName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([_currentTagName isEqualToString:@"city"]) {
_notes = [NSDictionary dictionaryWithObjectsAndKeys:string,@"city",nil];
}
if ([_currentTagName isEqualToString:@"data_1"]) {
_notes = [NSDictionary dictionaryWithObjectsAndKeys:string,@"data_1",nil];
}
}/*- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSString *str=[[NSString alloc] initWithString:_element];
NSLog(@"str");
}*/
//遇到文档结束时候触发
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil];
NSLog(@"%@",[_notes objectForKeyedSubscript:@"city"]);
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}@end对服务器的数据进行xml解析,将得到的数据存到NSDictionary中,最后我想将需要的数据显示到labal上,但是在labal上显示null,弄了好久都没解决,求大神们帮忙,多谢了。
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self start];
NSLog(@"%@",[_notes objectForKey:@"data_1"]);
_labal.text=[NSString stringWithFormat:@"%@%@的天气状况",[_notes objectForKey:@"data_1"], [_notes objectForKey:@"city"]];
}
-(void)start{
// 1. url确定资源
NSURL *url = [NSURL URLWithString:@"http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100"];
// 2. request建立请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 发送异步请求,新建数据处理队列,待数据处理完成后,再更新UI
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 1> 实例化XML解析器
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
// 2> 设置解析器的代理
//parser.delegate = self;
[parser setDelegate:self];
// 3> 开始解析
[parser parse];
}];
}//文档开始的时候触发
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
_notes=[NSDictionary new];
}//文档出错的时候触发
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%@",parseError);
}//遇到一个开始标签时候触发
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict{
_currentTagName=elementName;
NSLog(@"_currentTagName=%@",_currentTagName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([_currentTagName isEqualToString:@"city"]) {
_notes = [NSDictionary dictionaryWithObjectsAndKeys:string,@"city",nil];
}
if ([_currentTagName isEqualToString:@"data_1"]) {
_notes = [NSDictionary dictionaryWithObjectsAndKeys:string,@"data_1",nil];
}
}/*- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSString *str=[[NSString alloc] initWithString:_element];
NSLog(@"str");
}*/
//遇到文档结束时候触发
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil];
NSLog(@"%@",[_notes objectForKeyedSubscript:@"city"]);
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}@end对服务器的数据进行xml解析,将得到的数据存到NSDictionary中,最后我想将需要的数据显示到labal上,但是在labal上显示null,弄了好久都没解决,求大神们帮忙,多谢了。
/code]就是labal上无法获取到数据。