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方法中,解析一个大的可变字符串(如果结束元素是该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 回答