gpt4 book ai didi

ios - 为什么我从 xml Web 服务器对 iOS 的响应中得到终止错误?

转载 作者:行者123 更新时间:2023-11-28 22:35:33 27 4
gpt4 key购买 nike

我收到这样的回复:

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];
}

谢谢

最佳答案

您的代码假设 foundCharacters 只会为给定的标签调用一次。从日志输出来看,这不是真的。

在您的 didStartElement 中,初始化一个可变字符串。在 foundCharacters 方法中附加到这个可变字符串。

然后在didEndElement 方法中,解析一个大的可变字符串(如果结束元素是string 元素)。

这是我会做的:

添加私有(private)属性:@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];
}
}

关于ios - 为什么我从 xml Web 服务器对 iOS 的响应中得到终止错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16157261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com