iOS获取天气情况的总结
来源:互联网 发布:java多线程异步调用 编辑:程序博客网 时间:2024/04/28 23:43
最近在整理各种关于天气获取的方法,现在将学习到的东西进行总结。其实,天气获取的实例,主要分为两个部分,第一个部分就是通过网络获取到数据,第二个 部分是获取到数据后的分析处理。处理之后就是你自己开发的程序的问题,如何显示得更加的漂亮。
第一个部分关于数据获取。
通过网络获取数据的方法,有两种,一种是通过NSRUL,NSURLREQUEST,NSURLCONNECTION的方法获取。另一种方法是直接通过NSData的initContentwithURL或dataWithContentsOfURL的方法。
描述如下:
方法1:
NSString *googleURL =@"http://www.weather.com.cn/data/cityinfo/101070101.html";
NSURL *url = [NSURL URLWithString:googleURL];
NSURLRequest *request = [[NSURLRequestalloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnectionalloc] initWithRequest:request delegate:self];
[connectionrelease];
[request release];
委托的处理方法:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
outString = [[NSStringalloc] initWithData:dataencoding: NSUTF8StringEncoding];
NSLog(@"%@",outString);
}
-(void) connection:(NSURLConnection *)connection
didFailWithError: (NSError *)error {
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {
}
方法2:
#define kWeatherServiceURLStr @"http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?heCityName="
NSString *RequestUrlStr = [NSStringstringWithFormat:@"%@%@",kWeatherServiceURLStr,[[ipCityLocationcitySimpleName]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"request = %@", RequestUrlStr);
NSData * ReponseData = [NSDatadataWithContentsOfURL:[NSURLURLWithString:RequestUrlStr]];
第二部分为获取数据后的,数据分析,数据部分通常来讲也有两种方法,第一种是获取到的数据为JSON,第二种方法获取到的数据为XML。
方法1:获取到的数据为JSON。
iOS5之前并没有提供比较好的JSON类库,但是ios5之后就提供了一种公共的方法NSJSONSerialization,而为了保证ios5之前的方法也可以使用,需要使用的第三方库有SBJson和TouchJSON。不过好像SBJson用的人比较多。具体方法实例如下:
TouchJSON:
//获取API接口
- NSURL*url=[NSURLURLWithString:@"http://m.weather.com.cn/data/101010100.html"];
- //定义一个NSError对象,用于捕获错误信息
- NSError*error;
- //
- NSString*jsonString=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
- //将解析得到的内容存放字典中,编码格式UTF8,防止取值时候发生乱码
- NSDictionary*rootDic=[[CJSONDeserializerdeserializer]deserialize:[jsonStringdataUsingEncoding:NSUTF8StringEncoding]error:&error];
- //因为返回的Json文件有两层,去第二层类容放到字典中去0
- NSDictionary*weatherInfo=[rootDicobjectForKey:@"weatherinfo"];
- //取值打印
- NSLog(@"今天是%@%@%@的天气状况是:%@%@",[weatherInfoobjectForKey:@"date_y"],[weatherInfoobjectForKey:@"week"],[weatherInfoobjectForKey:@"city"],[weatherInfoobjectForKey:@"weather1"],[weatherInfoobjectForKey:@"temp1"]);
- NSURL*url=[NSURLURLWithString:@"http://m.weather.com.cn/data/101180701.html"];
- NSError*error=nil;
- NSString*jsonString=[NSStringstringWithContentsOfURL:urlencoding:NSUTF8StringEncodingerror:&error];
- SBJsonParser*parser=[[SBJsonParseralloc]init];
- NSDictionary*rootDic=[parserobjectWithString:jsonStringerror:&error];
- NSDictionary*weatherInfo=[rootDicobjectForKey:@"weatherinfo"];
- NSLog(@"今天是%@%@%@的天气状况是:%@%@",[weatherInfoobjectForKey:@"date_y"],[weatherInfoobjectForKey:@"week"],[weatherInfoobjectForKey:@"city"],[weatherInfoobjectForKey:@"weather1"],[weatherInfoobjectForKey:@"temp1"]);
- NSError*error;
- //加载一个NSURL对象
- NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://m.weather.com.cn/data/101180601.html"]];
- //将请求的url数据放到NSData对象中
- NSData*response=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
- //iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
- NSDictionary*weatherDic=[NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];
- //weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
- NSDictionary*weatherInfo=[weatherDicobjectForKey:@"weatherinfo"];
- NSLog(@"今天是%@%@%@的天气状况是:%@%@",[weatherInfoobjectForKey:@"date_y"],[weatherInfoobjectForKey:@"week"],[weatherInfoobjectForKey:@"city"],[weatherInfoobjectForKey:@"weather1"],[weatherInfoobjectForKey:@"temp1"]);
- //打印出weatherInfo字典所存储数据
- NSLog(@"weatherInfo字典里面的内容是--->%@",[weatherInfodescription]);
4.运行结果(如果想知道每次字符串和字典间取值情况,只需NSLog打印输出就行):
1、取值时发生应用程序崩溃,获取值不正确
有时我们从字典中获取了这样的数据,感觉比较郁闷,并未显示中文,这种情况是我们把数据放到字典中,编码方式是UTF8,取值打印出来的时候就成中文了
在解析出来数据后我想这样取值,
NSDictionary*weatherInfo = [rootDicobjectForKey:@"weatherinfo"];
NSArray*weatherArray = [rootDicobjectForKey:@"weatherinfo"];
for(NSDictionary*dicinweatherArray) {
NSLog(@"----->%@",dic);
}
打印出来的dic数据是这样的NSLog(@"----->%@",[dicobjectForKey:@"city"]);来取出city的值,但是应用程序崩溃
xmlParser = [[NSXMLParseralloc] initWithData:weatherReponseData];
[xmlParsersetDelegate:self];
[xmlParsersetShouldProcessNamespaces:NO];
[xmlParsersetShouldReportNamespacePrefixes:NO];
[xmlParsersetShouldResolveExternalEntities:NO];
// 启动解析命令
- (void)startParse{
//开始解析
[xmlParserparse];
}
// 设定委托方法
#pragma mark NSXMLParserDelegate
#pragma mark xml parser delegate
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSCharacterSet *whitespace = [NSCharacterSetwhitespaceCharacterSet];
string = [stringstringByTrimmingCharactersInSet:whitespace];
if (![stringisEqualToString:@"\n"]) {
[xmlWeatherStringArrayaddObject:string];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"%@", [parseErrordescription]);
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
[selfoutputParseInfo];
//取出xml中数据后提取信息
[selfsplitXmlWeatherInfo];
}
- iOS获取天气情况的总结
- iOS获取天气情况的总结
- flex webservice获取城市的天气情况
- webservice 获取天气情况
- [一天一项目]获取当前天气——获取某个地区当前的天气情况。
- 免费的获取最近几天内的天气情况
- ios 天气接口的获取 json实现
- java调用webservice获取指定城市天气情况的例子
- 在安卓中获取一个城市的天气情况
- 如何联网获取实时天气情况的小练习
- android webservice获取城市天气情况
- android webservice获取城市天气情况
- android webservice获取城市天气情况
- android webservice获取城市天气情况
- php 通过 SoapClient 获取天气情况
- python获取城市天气情况案例
- ajax获取当前城市天气情况
- 根据API获取天气情况进行解析
- Win CE下编译选项介绍
- 【转】Java为什么能跨平台,.NET为什么能跨语言?
- MongoDB整合Spring 详细讲解(含代码)
- [转]HashMap和Hashtable的区别
- windows mobile 编译(生成镜像)提速
- iOS获取天气情况的总结
- Topcoder SRM 565 Div.2
- ARM-Tiny6410-开发环境搭建-Hardware && Linux && arm-linux-gcc
- MySQL ERROR Nr.1045
- XIP与HIVE
- 手机耳机孔通信研究
- 我的微博
- tnsnames.ora配置小结
- winCE改变字库方法