0

こんにちは、クライアント サーバーの iPhone アプリケーションを作成しています。nsxml を使用して Web サイトから xml フィードを読み取ります。php サービスを作成する前に、別のサイトの rss で試してみましたが、うまくいきました。

それでも、サービスを書いた後、エラーコード76が表示され続けました

だから私がしたことは、私のPHPページから以前に別のWebサイトから受信することに成功したのとまったく同じRSSフィードをエコーすることです..そして、それはまだそれを読むことを拒否します..そして、同じ76コードエラーが表示されます!!!! XML 解析エラー: Web サイトからストーリー フィードをダウンロードできません (エラー コード 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");
}

ここで、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];
}

次に、解析デリゲート メソッドを使用する必要があります。正常に動作することを願っています

于 2011-12-06T11:00:18.787 に答える