2

以前にこれについて同様の質問をしましたが、あまり助けが得られず、さらに詳しく調べましたが、なぜ問題があるのか​​ わかりません.

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-1NSJSONSerialization文字列を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

だから私は見responseJSONAFJSONRequestOperation.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 フィードでも問題なく機能し、他のフィードからも正常に機能します。

http://datapoint.metoffice.gov.uk/でも

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

Sóil Chaorainn問題の原因となった地名を含む

4

1 に答える 1

0

問題は、応答のテキスト エンコーディングが間違っていることのようです。を使用してエンコードできない文字があります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;
}
于 2013-03-15T12:05:37.357 に答える