【发布时间】:2013-09-01 10:24:35
【问题描述】:
当我在模拟器中运行我的应用程序时,一切正常。但是当我在 Ipad 中运行相同的应用程序时,会抛出异常。
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零。
在我的应用程序中,我必须请求一个 web-URl 并且需要解析返回的 JSON 响应。但是我已经检查了 web-url 并且能够在模拟器中完美解析。但是所有问题都出现在真正的ios设备中。但是我想我已经确定了出错的代码。
+ (NSDictionary*) getParsedJSON:(NSString*) urlString {
NSLog(@"################################################################################");
NSLog(@"getParsedJSON => urlString:");
NSLog(@"%@", urlString);
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLResponse *response1 = nil;
NSError *error = nil;
NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response1 error:&error];
//NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"--------------------------------------------------------------------------------");
NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"getParsedJSON => responseString:\n%@", responseString);
NSLog(@"--------------------------------------------------------------------------------");
NSError* jsonParsingError = nil;
NSDictionary* parsedJSON = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; // here is place where exception seems to be thrown.
if (jsonParsingError) {
NSLog(@"ERROR in parsing JSON: %@", jsonParsingError);
} else {
NSLog(@"getParsedJSON => parsedJSON: \n%@", [parsedJSON description]);
}
NSLog(@"################################################################################");
return parsedJSON;
}
我已经确定了似乎是错误的行。我还附上了异常报告的屏幕截图。。希望您有经验的答复。
【问题讨论】:
-
发送请求后可以查看error是否为nil。如果不是,请打印错误 userInfo 以了解发生了什么。
-
您还应该检查
sendSynchronousRequest不为零。 -
@serrrgi 我已经检查过错误是否为零。但是就像我说的所有问题似乎都在设备中
-
设备连接到互联网了吗?
-
是的。设备已连接......因为如果互联网出现任何问题,应用程序应该在模拟器中抛出异常......
标签: iphone ios json ipad ios-simulator