【问题标题】:Getting Argument Exception (nil URL argument)!获取参数异常(无 URL 参数)!
【发布时间】:2012-06-13 01:17:54
【问题描述】:

我遇到了这个问题,我的 iPhone 上出现了这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData initWithContentsOfURL:options:error:]: nil URL argument'

我正在尝试解析一些 XML 数据,当我在模拟器上尝试时,我没有收到任何错误,但每当我在 iOS 设备上尝试时,我的控制台中都会出现错误!

这是我解析 XML 的代码:

- (ICB_WeatherConditions *)initWithQuery:(NSString *)query {

if (self = [super init])
{
    CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", query]] options:0 error:nil] autorelease];

    condition         = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/condition" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];        
    location          = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_information/city" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
    day2c    = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/condition" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
    day3c     = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/condition" error:nil] objectAtIndex:1] attributeForName:@"data"] stringValue] retain];

    currentTemp       = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/temp_f" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];
    lowTemp           = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions[2]/low" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];
    highTemp          = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/high" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];

    conditionImageURL = [[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com%@", [[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/icon" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue]]] retain];
}

return self;}

这是我发送查询的代码:

- (void)showWeatherFor:(NSString *)query {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

ICB_WeatherConditions *weather = [[ICB_WeatherConditions alloc] initWithQuery:query];

self.conditionsImage = [[UIImage imageWithData:[NSData dataWithContentsOfURL:weather.conditionImageURL]] retain];

[self performSelectorOnMainThread:@selector(updateUI:) withObject:weather waitUntilDone:NO];


[pool release];}

请帮我指出正确的方向!再次感谢!

【问题讨论】:

  • 我检查了 XML 以查看它是否已启动,现在仍然如此。真正困扰我的是它在模拟器上而不是在设备上工作!我也在使用 TouchXML 进行解析。

标签: objective-c xml-parsing uncaught-exception initwithcontentsofurl


【解决方案1】:

让您开始:错误出现在您的if 语句的第一行。这会构建一个字符串和一个 URL,但“nil URL 参数”是个例外。所以把这条线分成几部分:

NSString *queryPath = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", query];
NSURL *queryURL = [NSURL URLWithString:queryPath];
CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:queryURL options:0 error:nil] autorelease];

现在输入代码来测试一切 - queryqueryPathqueryURL - 如果值不是您所期望的,则会产生错误。问题应该很明显了。

【讨论】:

  • 谢谢!我能够做到这一点,并发现是空间导致了这些错误。这使我能够弄清楚是什么区域导致了问题。感谢您的回复!非常感谢!
【解决方案2】:

我想通了!

好的,下面是故事和解决方案: 我正在制作一个天气应用程序,查询需要一个城市才能获得包含数据的有效 XML 文件。在我的模拟器中,位置很差,所以它把我放在加利福尼亚州格伦代尔?!如您所见格伦代尔是一个词。因此,当它进入查询时,它作为有效 URL 传递,并获取数据。当我去我的手机测试它时,我得到了上面所说的错误!这是因为我住的地方,从 CLGeocoder 的 reverseGeocodeLocation 检索到的城市名称是 2 个单词。 (即得梅因)。 中间的空间搞砸了一切。显然,在 URL 中放置一个空格不会让您返回任何数据。为了在我使用的“XML spitting service”中获得空间,您必须使用 a + 符号。所以经过大约4个小时的调试,我发现了这个,我把这个放进去:

 stringByReplacingOccurrencesOfString:@" " withString:@"+"

在我的方法的最后,所以最后它看起来像这样:

 City = [[placemark locality] stringByReplacingOccurrencesOfString:@" " withString:@"+"];

这解决了它,现在可以用加号替换空格,从而返回实际数据!祝遇到类似问题的人好运!

【讨论】:

    猜你喜欢
    • 2014-01-12
    • 2022-11-24
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多