【问题标题】:AFJSONRequestOperation crashing with "data parameter is nil" NSJSONSerialization errorAFJSONRequestOperation 因“数据参数为 nil”而崩溃 NSJSONSerialization 错误
【发布时间】:2013-03-15 11:35:23
【问题描述】:

我之前就这个问题问过一个类似的问题,但没有得到太多帮助,现在进一步研究了它,仍然看不出我为什么会遇到问题。

NSURL *url = [NSURL URLWithString:@"http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/sitelist?res=daily&key=<MY API KEY>"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Success");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Fail");
}];
[operation start];

以下内容失败

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

我认为问题是由于返回为 ISO-8859-1 的 JSON 类型,我设法通过将重新调整的字符串编码为 NSUTF8StringEncoding 来使用 NSJSONSerialization

示例...

NSString *string = [NSString stringWithContentsOfURL:kMetOfficeAllSites encoding:NSISOLatin1StringEncoding error:&error];
NSData *metOfficeData = [string dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerialization JSONObjectWithData:metOfficeData options:kNilOptions error:&error];
if (error) {
    //Error handling
} else {
    //use JSON

所以我在AFJSONRequestOperation.m 中查看了responseJSON

- (id)responseJSON {
[self.lock lock];
if (!_responseJSON && [self.responseData length] > 0 && [self isFinished] && !self.JSONError) {
    NSError *error = nil;

    // Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization.
    // See https://github.com/rails/rails/issues/1742
    if ([self.responseData length] == 0 || [self.responseString isEqualToString:@" "]) {
        self.responseJSON = nil;
    } else {
        // Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character
        // See http://stackoverflow.com/a/12843465/157142
        NSData *JSONData = [self.responseString dataUsingEncoding:self.responseStringEncoding];
        self.responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:self.JSONReadingOptions error:&error];
    }

    self.JSONError = error;
}
[self.lock unlock];

return _responseJSON;
}

代码在else 语句中崩溃,但这似乎与我之前直接使用NSJSONSerialization 并重新编码responseString 时所做的一样。

我什至将dataUsingEncoding 硬编码为NSUTF8StringEncoding,但它仍然崩溃,我不明白为什么?

注意:以上内容适用于其他 JSON 提要,也适用于

上的其他提要

http://datapoint.metoffice.gov.uk/ 但是

http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/sitelist?res=daily&key=

包括导致问题的地名Sóil Chaorainn

【问题讨论】:

    标签: iphone ios objective-c afnetworking


    【解决方案1】:

    问题似乎是响应的文本编码错误。有些字符无法使用ISO-8859-1 进行编码。这就是responseString 方法(参见AFURLConnectionOperation.m)返回nil 并且JSON 序列化失败的原因。

    要解决此问题,您可以继承 AFJSONRequestOperation 并以这种方式覆盖 responseStringEncoding 以强制执行 UTF-8 编码:

    - (NSStringEncoding)responseStringEncoding {
        [self.lock lock];
        if (!_responseStringEncoding && self.response) {
            self.responseStringEncoding = NSUTF8StringEncoding;
        }
        [self.lock unlock];
    
        return _responseStringEncoding;
    }
    

    【讨论】:

    • 和我尝试 NSData *JSONData = [self.responseString dataUsingEncoding: NSUTF8StringEncoding];
    • 否,因为 responseString 方法使用 responseStringEncoding 本身,因此将为 nil。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 2020-09-29
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多