AFJSONRequestOperation

AFJSONRequestOperation

我以前曾在此问题上提出过类似的问题,但并没有获得太多帮助,现在已经对其进行了进一步调查,但仍然看不到为什么会有问题。

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'

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

例...
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

所以我看着responseJSON中的AFJSONRequestOperation.m
- (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供稿,也适用于其他JSON供稿

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

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

包括引起问题的地名Sóil Chaorainn

最佳答案

问题似乎是响应的文本编码错误。有些字符无法使用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;
}

10-08 05:32