如何使用NSXML解析Google weather API? [英] How to parse Google weather API using NSXML?

查看:123
本文介绍了如何使用NSXML解析Google weather API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSXML解析谷歌天气API,所以请给我一些指导。

i want to parse google weather API using NSXML so please give me some Guidance for this.

这是我的网址

我采取了这样的措施:

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您可以获得此代码,如果它对您有帮助,则无需建立连接

hey ankit you can get this code if at all its helpful to you no need to establish connection

只需使用此方法

-(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];
    }
}

- (void)解析器:(NSXMLParser *)解析器foundCharacters:(NSString *)string {
if(tmpString == nil&&!didGetHTML){
tmpString = [[NSString alloc] initWithString:string];
} else if(!didGetHTML){
NSString * t = [NSString stringWithString:tmpString];
if([tmpString retainCount]> 0){[tmpString release]; tmpString =无; }
tmpString = [[NSString alloc] initWithFormat:@%@%@,t,string];
}
}

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string { if(tmpString==nil && !didGetHTML){ tmpString=[[NSString alloc] initWithString:string]; } else if(!didGetHTML){ NSString *t=[NSString stringWithString:tmpString]; if([tmpString retainCount]>0) { [tmpString release]; tmpString=nil; } 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方法你已经编写了这个方法

and simply call the initwith url method from which ever class you have written this method

只需要给出特定对象标签的根标签对象标签和元素标签,然后在字典中获取响应后给出选择器它们将它取为数组,并根据键值

just you have to give root tag object tag and element tag of a particular object tag and also give selector after that take the response in dictionary and they take it in array and display the result according to your value for key

这篇关于如何使用NSXML解析Google weather API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆