0

次のような応答が得られます。

2013-04-22 15:38:38:693 Some[963:3079] __36-[SDAFParseAPIClient getDataFromWeb]_block_invoke [Line 88] parsing
2013-04-22 15:38:38:694 Some[963:3079] -[SDAFParseAPIClient parser:didStartElement:namespaceURI:qualifiedName:attributes:] [Line 64] Element started string
2013-04-22 15:38:38:695 Some[963:3079] -[SDAFParseAPIClient parser:foundCharacters:] [Line 78] The characters are { "recordcount": "62", "data": [ { "Codigo": "1", "NombrePublico": "Super Stor
2013-04-22 15:38:38:696 Some[963:3079] -[SDAFParseAPIClient parser:foundCharacters:] [Line 80] The page numbers are (null)
2013-04-22 15:38:38:698 Some[963:3079] -[SDAFParseAPIClient parser:foundCharacters:] [Line 78] The characters are es", "Horario": "LV:8:00 AM - 6:30 PM,S:8:00 AM - 5:00 PM,D:-", "Direccion": "6 Ave., 5 Calle, S.O. Bo. El", "Telefono": "2556-1242", "Coordenadas": "16.502917616794166,-85.02710920572281", "Ciudad": "SAN PEDRO SULA" }, { "Codigo": "2", "NombrePublico": "Some 3 Ave.", "Horario":
2013-04-22 15:38:39.571 Some[963:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

これが私のコードです:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    NSLog(@"Element started %@",elementName);
    self.currentElement=elementName;

}

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

    NSLog(@"Element ended %@",elementName);
    self.currentElement=@"";

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if([self.currentElement isEqualToString:@"string"]){
        NSLog(@"The characters are %@",string);
        id data = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSASCIIStringEncoding] options:0 error:nil];
        NSLog(@"The page numbers are %@",[[[data valueForKey:@"List"] objectAtIndex:0] valueForKey:@"PageNumber"]);
    }
}

-(void)getDataFromWeb{
    NSURL *feedURL = [[NSURL alloc] initWithString:@"http://someserver.com?Cli_Cod=1&Key_String=34uhg934hg389j"];
    NSURLRequest *feedRequest = [[NSURLRequest alloc] initWithURL:feedURL];
    AFXMLRequestOperation *feedOperation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:feedRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
        NSLog(@"parsing");
        [XMLParser setDelegate:self];
        [XMLParser parse];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
        UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [connectionError show];
    }];
    [feedOperation start];
}

ありがとう

4

1 に答える 1

2

foundCharactersあなたのコードは、特定のタグに対して一度だけ呼び出されることを前提としています。ログ出力から、これは正しくありません。

didStartElementで、変更可能な文字列を初期化します。メソッドでこの変更可能な文字列に追加しますfoundCharacters

次に、didEndElementメソッドで、1 つの大きな変更可能な文字列を解析します (最後の要素が要素の場合string)。

これは私がすることです:

プライベート プロパティを追加します。@property (nonatomic, strong) NSMutableString *currentValue;

次に、メソッドを更新します。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Element started %@",elementName);
    if ([elementName isEqualToString:@"string"]) {
        self.currentElement = elementName;
        self.currentValue = [NSMutableString string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"Element ended %@",elementName);
    if ([self.currentElement isEqualToString:@"string"]) {
        NSLog(@"The characters are %@", self.currentValue);
        id data = [NSJSONSerialization JSONObjectWithData:[self.currentValue dataUsingEncoding:NSASCIIStringEncoding] options:0 error:nil];
        NSLog(@"The page numbers are %@", [[[data valueForKey:@"List"] objectAtIndex:0] valueForKey:@"PageNumber"]);
    }

    self.currentElement = nil;
    self.currentValue = nil;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (self.currentValue) {
        [self.currentValue appendString:string];
    }
}
于 2013-04-22T21:58:57.953 に答える