iPhone NSXML解析器的布尔值

问题描述

| 我正在通过从XML文件http://www.calvaryccm.com/ServiceTimes.asmx/IsServiceTime读取来确定事件是否正在直播。 它返回一个布尔结果,但是我无法弄清楚如何使用Objective-C访问它。这是我到目前为止所拥有的:
NSError * error = nil;
NSString * responseString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@\"http://www.calvaryccm.com/ServiceTimes.asmx/IsServiceTime\"] encoding:NSUTF8StringEncoding error:&error];
NSLog(responseString);
if (error) {
    NSLog(@\"%@\",[error localizedDescription]);
}

if ([responseString isEqualToString:@\"true\"]) {
    // Handle active content.
    NSString *baseVideoUrl = @\"http://streaming5.calvaryccm.com:1935/live/iPhone/playlist.m3u8\";
    NSLog(@\" finalUrl is : %@\",baseVideoUrl);

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:baseVideoUrl]];

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
        // Use the 3.2 style API  
        moviePlayer.controlStyle = MPMovieControlStyleDefault;  
        moviePlayer.shouldAutoplay = YES;  
        [self.view addSubview:moviePlayer.view];  
        [moviePlayer setFullscreen:YES animated:YES];  
    }

} else {
    // Inform user that the content is unavailable
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @\"Live Service\"
                          message: @\"There is no service going on at this time\"
                          delegate: nil
                          cancelButtonTitle:@\"OK\"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
    

解决方法

        首先,您的NSLog语句应为:
NSLog(@\"%@\",responseString); 
这将正确注销响应字符串,然后您将返回该值(我刚刚尝试过):
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<boolean xmlns=\"http://tempuri.org/\">false</boolean>
您的响应字符串不等于true,因为它是完全限定的XML字符串表示形式。您需要搜索字符串,可以通过查找字符串是否包含文本“ true”或“ false \”来实现。
NSRange range = [responseString rangeOfString : @\"true\"];

if (range.location != NSNotFound) {
    //String contained true
}
或者,您可以使用轻量级的XML解决方案,例如GDataXMLNode     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...