WebService不完整的目标-C

时间:2013-08-20 23:46:29

标签: ios xml-parsing webservice-client

我在我的应用程序上解析了一个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但由于数据长度包含数据,任何帮助我都会感激

提前致谢

1 个答案:

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

我所做的只是:

  1. 获取NSString(XMLResponse)并将编码更改为NSISOLatin1StringEncoding
  2. 通过使用NSString(XMLResponse)一步创建NSData对象来初始化NSXMLParser对象(解析),但现在使用NSUTF8StringEncoding进行编码
  3. 尝试并告诉我们它是怎么回事!