簡體   English   中英

如何使用NSXML解析Google weather API?

[英]How to parse Google weather API using NSXML?

我想使用NSXML解析Google Weather API,所以請為此提供一些指導。

這是我的網址

我已經采取了這樣的步驟:

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

}

嗨,如果您的代碼對您完全沒有幫助,則可以獲取此代碼

只是使用這種方法

-(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)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 keepCount]> 0){[tmpString版本]; 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方法

只是您必須給根標簽對象標簽和特定對象標簽的元素標簽,然后給選擇器,然后在字典中獲取響應,然后將其接受到數組中,並根據您的鍵值顯示結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM