如何使用NSXML解析Google天气API?
问题描述:
我想用NSXML解析谷歌天气API,所以请给我一些指导。如何使用NSXML解析Google天气API?
这是My url
,我已经采取的步骤,类型:
NSURL *url = [NSURL URLWithString:@"http://www.google.com/ig/api?weather=Ahemdabad"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection){
webData = [[NSMutableData data] retain];
NSLog(@"connection established");
}
else {
NSLog(@"theConnection is NULL");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connectio
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"thexml=============>%@", theXML);
[theXML release];
if(parser)
{
[parser release];
}
parser = [[NSXMLParser alloc]initWithData:webData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[webData release];
}
答
哎ANKIT你可以得到这个代码,如果在其所有帮助你没有必要建立连接
只用这种方法
-(id)initWithURL:(NSURL*)url arrayRootObjectTags:(NSArray*)arrTags sel:(SEL)seletor andHandler:(NSObject*)handler{
if(self = [super init]){
self.mainArray=arrTags;
self.MainHandler=handler;
self.targetSelector=seletor;
NSLog(@"%@",[url description]);
NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:30];
con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con){
myWebData=[[NSMutableData data] retain];
} else {
[MainHandler performSelector:@selector(targetSelector:) withObject:nil];
}
}
return self;
}
还有其他配套方法
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict {
if([elementName isEqualToString:@"html"] || [elementName isEqualToString:@"HTML"]){
didGetHTML=YES; [self parserDidEndDocument:parser];
} else if([[mainArray objectAtIndex:0] isEqualToString:elementName] && [[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML) {
objectsArray=[[NSMutableArray alloc] init];
tmpD=[[NSMutableDictionary alloc] init];
if(tmpOther==nil) tmpOther=[[NSMutableDictionary alloc] init];
} else if([[mainArray objectAtIndex:0] isEqualToString:elementName] && !didGetHTML) {
objectsArray=[[NSMutableArray alloc] init];
if(tmpOther==nil) tmpOther=[[NSMutableDictionary alloc] init];
} else if([[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML) {
tmpD=[[NSMutableDictionary alloc] init];
} else if([mainArray containsObject:elementName] && !didGetHTML){
[tmpD setValue:[attributeDict valueForKey:@"data"] forKey:elementName];
}
}
- (空)解析器:(*的NSXMLParser)分析器foundCharacters:(的NSString *)字符串{ 如果(tmpString ==零& & didGetHTML!){ tmpString = [[NSString alloc] initWithString:string];其他如果(!didGetHTML){NBRtring * t = [NSString stringWithString:tmpString]; if([tmpString retainCount]> 0){[tmpString release]; tmpString =无; } tmpString = [[NSString alloc] initWithFormat:@“%@%@”,t,string]; } }
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName {
if([[mainArray objectAtIndex:0] isEqualToString:elementName] && [[mainArray objectAtIndex:1] isEqualToString:elementName] && !didGetHTML){
[objectsArray addObject:tmpD];
} else if([elementName isEqualToString:[mainArray objectAtIndex:1]] && !didGetHTML){
[objectsArray addObject:tmpD];
[tmpD release]; tmpD=nil;
} else if([mainArray containsObject:elementName] && !didGetHTML) {
if(![tmpD valueForKey:elementName]){
[tmpD setValue:tmpString forKey:elementName];
}
[tmpString release]; tmpString=nil;
} else {
[tmpOther setValue:tmpString forKey:elementName];
[tmpString release]; tmpString=nil;
}
}
,并简单地调用从中不断类,你写了这个方法
只是你必须给根标签对象标签和特定对象标记的元素标签,也是initwith URL方法给选择器后,在字典中的响应,他们把它在阵列中,并显示结果根据您的值为键
**谷歌的天气API在2012年关闭** - > http://stackoverflow.com /问题/ 12145820 /谷歌全天候-API不见了/ 35943521 – 2016-03-11 15:56:56