0

嗨,我正在编写一个客户端服务器 iphone 应用程序,我使用 nsxml 从网站读取 xml 提要..在我编写 php 服务之前,我用另一个站点的 rss 尝试过它......它工作得很好。

然而,在我编写服务之后,它一直给我错误代码 76

所以我所做的是回显与我之前从我的 php 页面成功从另一个网站接收到的完全相同的 rss 提要..它仍然拒绝阅读它..它给了我相同的 76 代码错误!!!!解析 XML 时出错:无法从网站下载故事提要(错误代码 76)

 - (void)parserDidStartDocument:(NSXMLParser *)parser { 
    NSLog(@"found file and started parsing"); 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { 
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]]; 
    NSLog(@"error parsing XML: %@", errorString); 
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [errorAlert show]; 
} 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    //NSLog(@"found this element: %@", elementName);
    currentElement = [elementName copy]; 
    if ([elementName isEqualToString:@"item"]) { 
        // clear out our story item caches... 
        item = [[NSMutableDictionary alloc] init]; 
        currentTitle = [[NSMutableString alloc] init]; 
        currentDate = [[NSMutableString alloc] init]; 
        currentSummary = [[NSMutableString alloc] init]; 
        currentLink = [[NSMutableString alloc] init]; 
    } 
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
    //NSLog(@"ended element: %@", elementName);
    if ([elementName isEqualToString:@"item"]) { 
    // save values to an item, then store that item into the array...
        [item setObject:currentTitle forKey:@"title"]; 
        [item setObject:currentLink forKey:@"link"];
        [item setObject:currentSummary forKey:@"summary"]; 
        [item setObject:currentDate forKey:@"date"]; 
        [stories addObject:[item copy]]; 
        NSLog(@"adding story: %@", currentTitle); 
    } 
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
 //NSLog(@"found characters: %@", string); 
// save the characters for the current item... 
    if ([currentElement isEqualToString:@"title"]) { 
        [currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
        [currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"description"]) { 
        [currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
        [currentDate appendString:string]; 
    } 
}


- (void)parserDidEndDocument:(NSXMLParser *)parser { 
    NSLog(@"all done!"); 
    NSLog(@"stories array has %d items", [stories count]);
    // [newsTable reloadData]; 
}
4

1 回答 1

0

海你可以使用下面的代码,但要确保你正在做 NSXMLParserDelegate

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
//  NSLog(@"theConnection.........%@",theConnection);
    webData = [[NSMutableData data] retain];
}
else
{
    //  NSLog(@"theConnection is NULL");
}

这里的请求包含 URL Web 服务。然后添加以下代码:

-(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 {
[connection release];
[webData release];}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
if( xmlParser )
{
    [xmlParser release];
}

xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];

[connection release];
[webData release];
}

然后你应该使用你的解析委托方法。我希望它能正常工作

于 2011-12-06T11:00:18.787 回答