NSString *weatherUrl=[NSString stringWithFormat:@"%@%@.html",@"http://www.weather.com.cn/data/cityinfo/",cityID];
DLog(@"the url is %@",weatherUrl);
NSURL *url = [NSURL URLWithString:weatherUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *request, NSData *data, NSError *err) {
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
DLog(@"weather json is %@",dic);
NSMutableDictionary *jsonoSubObj = [dic objectForKey:@"weatherinfo"];
[self.todayWeather setText:[[NSString alloc] initWithFormat:@"%@\n",[jsonoSubObj objectForKey:@"weather"]]];
[self setWeatherIcon:[self.todayWeather text]];
[self.todayWeatherNum setText:[NSString stringWithFormat:@"%@-%@",[jsonoSubObj objectForKey:@"temp2"],[jsonoSubObj objectForKey:@"temp1"]]];
}];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self];
[connection start];
[connection release];
[request release];
获取天气信息的网络请求与解析
本文详细阐述了如何通过网络请求获取指定城市的天气信息,并解析JSON数据以展示当前天气状况,包括天气描述、温度范围等关键指标。
9

被折叠的 条评论
为什么被折叠?



