[英]iPhone NSXML Parser for a Boolean Value
我正在通过从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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.