(void)viewDidLoad {
[super viewDidLoad];
[self loadData];
}
-(void)loadData
{
//根据请求,加载网络数据
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/sk/101010100.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
//将二进制数据转换为字典
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@ 市温度 %@ 风向 %@ 风力 %@",result[@"weatherinfo"][@"city"],result[@"weatherinfo"][@"temp"],result[@"weatherinfo"][@"WD"],result[@"weatherinfo"][@"WS"]);
}];
}
只在viewcontroller.m 中写了这些代码,结果出错了,可是很多书上都是这么写的啊。真诚求解