Encoding issue: Cocoa Error 261?

Posted by Attacus on Stack Overflow See other posts from Stack Overflow or by Attacus
Published on 2009-12-18T22:44:40Z Indexed on 2010/05/21 5:10 UTC
Read the original article Hit count: 721

Filed under:
|
|
|

So I'm fetching a JSON string from a php script in my iPhone app using:

NSURL *baseURL = [NSURL URLWithString:@"test.php"];
NSError *encodeError = [[NSError alloc] init];
NSString *jsonString = [NSString stringWithContentsOfURL:baseURL encoding:NSUTF8StringEncoding error:&encodeError];
NSLog(@"Error: %@", [encodeError localizedDescription]);
NSLog(@"STRING: %@", jsonString);

The JSON string validates when I test the output. Now I'm having an encoding issue. When I fetch a single echo'd line such as:

{ "testKey":"é" }

(I'm aware I could\should be using NSUrlConnection for asynchronous fetching of data, but at this point in the app development, I don't really need it.)

The JSON parser works fine and I am able to create a valid JSON object. However, when I fetch my 2MB JSON string, I get presented with:

Error: Operation could not be completed. (Cocoa error 261.)

and a Null string. My PHP file is UTF8 itself and I am not using utf8_encode() because that seems to double encode the data since I'm already pulling the data as NSUTF8StringEncoding. Either way, in my single-echo test, it's the approach that allowed me to successfully log \ASDAS style UTF8 escapes when building the JSON object.

What could be causing the error in the case of the larger string?

Also, I'm not sure if it makes a difference, but I'm using the php function addslashes() on my parsed php data to account for quotes and such when building the JSON string.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about JSON