2011-01-10 145 views
0

我目前正在做一個使用來自http://www.learnerstogether.net/的RSS源的應用程序。但是,我不斷收到錯誤無法從網站下載XML數據。有人可以告訴我什麼是錯的。RSS閱讀器錯誤

@implementation Parser 
@synthesize items, responseData; 
@synthesize currentTitle; 
@synthesize currentDate; 
@synthesize currentSummary; 
@synthesize currentLink; 
@synthesize currentPodcastLink; 

- (void)parseRssFeed:(NSString *)url withDelegate:(id)aDelegate { 
    [self setDelegate:aDelegate]; 

    responseData = [[NSMutableData data] retain]; 
    NSURL *baseURL = [[NSURL URLWithString:url] retain]; 


    NSURLRequest *request = [NSURLRequest requestWithURL:baseURL]; 

    [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSString * errorString = [NSString stringWithFormat:@"Unable to download xml data (Error code %i)", [error code]]; 

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    self.items = [[NSMutableArray alloc] init]; 

    NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:responseData]; 

    [rssParser setDelegate:self]; 

    [rssParser parse]; 
} 

#pragma mark rssParser methods 

- (void)parserDidStartDocument:(NSXMLParser *)parser { 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    currentElement = [elementName copy]; 

    if ([elementName isEqualToString:@"item"]) { 
     item = [[NSMutableDictionary alloc] init]; 
     self.currentTitle = [[NSMutableString alloc] init]; 
     self.currentDate = [[NSMutableString alloc] init]; 
     self.currentSummary = [[NSMutableString alloc] init]; 
     self.currentLink = [[NSMutableString alloc] init]; 
     self.currentPodcastLink = [[NSMutableString alloc] init]; 
    } 

    // podcast url is an attribute of the element enclosure 
    if ([currentElement isEqualToString:@"enclosure"]) { 
     [currentPodcastLink appendString:[attributeDict objectForKey:@"url"]]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 

    if ([elementName isEqualToString:@"item"]) { 
     [item setObject:self.currentTitle forKey:@"title"]; 
     [item setObject:self.currentLink forKey:@"link"]; 
     [item setObject:self.currentSummary forKey:@"summary"]; 
     [item setObject:self.currentPodcastLink forKey:@"podcastLink"]; 

     // Parse date here 
     NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 

     [dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700 
     NSDate *date = [dateFormatter dateFromString:self.currentDate]; 

     [item setObject:date forKey:@"date"]; 

     [items addObject:[item copy]]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    if ([currentElement isEqualToString:@"title"]) { 
     [self.currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
     [self.currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"description"]) { 
     [self.currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
     [self.currentDate appendString:string]; 
     NSCharacterSet* charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@" \n"]; 
     [self.currentDate setString: [self.currentDate stringByTrimmingCharactersInSet: charsToTrim]]; 
    } 
} 

- (void)parserDidEndDocument:(NSXMLParser *)parser { 
    if ([_delegate respondsToSelector:@selector(receivedItems:)]) 
     [_delegate receivedItems:items]; 
    else 
    { 
     [NSException raise:NSInternalInconsistencyException 
        format:@"Delegate doesn't respond to receivedItems:"]; 
    } 
} 

#pragma mark Delegate methods 

- (id)delegate { 
    return _delegate; 
} 

- (void)setDelegate:(id)new_delegate { 
    _delegate = new_delegate; 
} 

- (void)dealloc { 
    [items release]; 
    [responseData release]; 
    [super dealloc]; 
} 
@end 
+0

你的問題太難讀了,請使用GDB控制檯的Back Trace命令編輯你正在崩潰的問題。 – Tirth 2011-01-10 06:42:42

回答

0

您的錯誤與此RSS解析方面完全無關。更重要的是,你甚至沒有粘貼完整的錯誤。您應該在執行-connection:didFailWithError:時將錯誤對象本身記錄下來。這應該給你一個人類可讀的錯誤描述。