2011-01-10 5 views
0

현재 RSS 피드를 사용하는 응용 프로그램을 http://www.learnerstogether.net/에서 만들고 있습니다. 그러나 웹 사이트에서 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

+0

iPhone에서 RSS를 구문 분석하려면 [FeedParser] (https://github.com/kballard/feedparser)를 살펴 보는 것이 좋습니다. –

답변

0

귀하의 오류는 RSS 구문 분석 측면과는 아무런 관련이 없습니다. 더 많은 것은, 당신은 가득 차있는 과실에 풀칠조차하지 않았다. 구현시 -connection:didFailWithError:에 오류 객체 자체를 기록해야합니다. 그렇게하면 무엇이 잘못되었는지 인간이 읽을 수있는 설명을 줄 수 있습니다.