nsxml可以工作但不能使用我的服务

时间:2011-12-06 10:24:22

标签: php iphone nsxml

我正在编写一个客户端服务器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]; 
}

1 个答案:

答案 0 :(得分:0)

Hai您可以使用以下代码,但请确保您正在执行NSXMLParserDelegate

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

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

此处theRequest包含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];
}

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

相关问题