【发布时间】:2014-06-25 17:38:46
【问题描述】:
我在运行应用程序时使用 Web 服务开发应用程序,它得到响应,但有时它崩溃并显示如下错误:
NSInvalidArgumentException',原因:'数据参数为零'
这是我的代码:
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.espn.com/v1/sports/tennis/news/headlines?apikey=th98vzm67pufc36ka42xxxmy"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
NSArray *headlinetop=[jsonArray objectForKey:@"headlines"];
for (int i=0; i<[headlinetop count]; i++)
{
NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
[loadingcell addObject:headstr];
}
【问题讨论】:
-
那么记录
responseData、err...呢?是否只有一个 URL 的数据为零? -
我得到了很好的响应,但有时它会像这样崩溃
-
好吧,当它崩溃时,检查
err可能会很有趣。我会使用另一个NSError,而不是使用两次err。不检查它们,就像把nil代替。
标签: ios objective-c