【问题标题】:Ok in simulator but throwing exception in ios device在模拟器中正常,但在 ios 设备中抛出异常
【发布时间】: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


【解决方案1】:

正如我们从日志中看到的,当您在设备上使用响应字符串时,您的响应字符串为空。这可能是由于某些 Internet 访问问题。尝试使用:

if([response isequaltostring:@"(null)"]||response == nil || response.length == 0)
{
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]);
     }
}

还尝试添加异常断点并发布应用程序崩溃的确切位置。 让我知道结果。

【讨论】:

    【解决方案2】:

    首先,您需要在 Xcode 中设置一个异常断点 - 这里有很多关于如何做到这一点的帖子。其次,在每个创建或返回对象的语句之后,添加一个断言:

    NSURL *foo = ...
    assert(foo);
    

    这样做将帮助您找到第一个问题,而不是最后一个问题。

    【讨论】:

      【解决方案3】:

      根据您的日志,您的响应字符串为空!

      做好以下两件事!

      • 添加NSLog(@"响应数据:%@",response);并检查响应是否有价值?
      • 如果 'response' 有值,将其转换为字符串 - 记录字符串值 - 并检查任何键是否有 nil 值?

      'NSJSONSerialization JSONObjectWithData' 方法如果找到任何值为 nil 的键就会崩溃。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-20
        • 1970-01-01
        • 2015-06-18
        • 1970-01-01
        相关资源
        最近更新 更多