我在我的应用程序上解析了一个webService响应(XML),我意识到并不是所有的答案都被解析了(我预计至少还有3个相同信息的块),所以我决定修改我的方法connectionDidFinishLoading就像这样:
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSString *XMLResponse = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"xml message: %@, data length: %i", XMLResponse, [webData length]);
if ([XMLResponse hasPrefix:@"[ERR]"]){
[[[UIAlertView alloc]initWithTitle:@"Notification"
message:XMLResponse != nil ? [@"there was an error while trying to load service" stringByAppendingString:XMLResponse]: @"there was an error while trying to load service"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil]show];
_transacWebServCompleto = @"FALSE";
return;
}
xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
在我运行我的应用程序时,我在输出部分
中得到了这个xml message: (null), data length: 5516
表格打印正确但不完整,我的XML显示为null但由于数据长度包含数据,任何帮助我都会感激
提前致谢
答案 0 :(得分:2)
你的XML可能包含特殊字符,如重音字,试试这个:
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSString *XMLResponse = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSISOLatin1StringEncoding];
NSLog(@"xml message: %@, data length: %i", XMLResponse, [webData length]);
if ([XMLResponse hasPrefix:@"[ERR]"]){
[[[UIAlertView alloc]initWithTitle:@"Notification"
message:XMLResponse != nil ? [@"there was an error while trying to load service" stringByAppendingString:XMLResponse]: @"there was an error while trying to load service"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil]show];
_transacWebServCompleto = @"FALSE";
return;
}
xmlParser = [[NSXMLParser alloc] initWithData:[XMLResponse dataUsingEncoding:NSUTF8StringEncoding]];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
我所做的只是:
尝试并告诉我们它是怎么回事!