获取参数异常(无 URL 参数)



我在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];}

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

为了帮助您入门:错误出现在 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 - 如果值不是您期望的,则会产生错误。问题应该变得明显。

我想通了!

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

 stringByReplacingOccurrencesOfString:@" " withString:@"+"

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

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

这解决了它,现在空格可以用加号替换,因此给我实际数据!祝任何有类似问题的人好运!

最新更新