开发者

Encoding issue: Cocoa Error 261?

开发者 https://www.devze.com 2022-12-14 15:05 出处:网络
So I\'m fetching a JSON string from a php script in my iPhone app using: NSURL *baseURL = [NSURL URLWithString:@\"test.php\"];

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":"é" }

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开发者_JS百科 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.


I'm surprised no one has mentioned using a different encoding value instead of NSUTF8StringEncoding when calling [NSString stringWithContentsOfFile:encoding:error:].

I also got Cocoa error 261 when parsing a JSON file. I just went through the list of NSString encodings until one worked. Fortunately the first one worked for me: NSASCIIStringEncoding!

You can also use NSString stringWithContentsOfFile:usedEncoding:error: to try to find the correct encoding (as described here: How to use stringWithContentsOfURL:encoding:error:?).


Don't know if this is your problem, but I just had a similar thing (stringWithContentsOfFile, no JSON), and the problem was that the file had CRLF (windows) line-endings and Western-whatever-it's-called encoding. I used SubEthaEdit to convert to LF (Mac/Unix line-endings) and UTF-8 encoding, and now everything works fine.


Encoding issue: Cocoa Error 261? I solved this issue by trying different encoding. First I was using NSUTF8 then I switched to NSASCIIStringEncoding and it worked.

NSString* path = [[NSBundle mainBundle] pathForResource: @"fileName" ofType: @"type"];
NSData* data = [NSData dataWithContentsOfFile:path];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",string);


For future reference, if you need to override the encoding, and you're working with streams without embedded NULs, something like this might be good (I've just written a rough sketch outline here, check this code is and does want you want before using it):

NSHTTPURLResponse* resp=nil;
NSData* jsonAsImmutableData = [NSURLConnection sendSynchronousRequest:
  [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://<whatever>"]]
  returningResponse:&resp error:NULL];

NSMutableData*modifiedData = [NSMutableData dataWithData:jsonAsImmutableData];

char extraNulls[7] =
  {0,0,0,0,0,0,0}; // is this defensive enough for your encoding?
[modifiedData appendBytes:extraNulls length:7];

NSString* jsonAsString = [NSString stringWithCString:[modifiedData bytes]
  encoding:<whatever your encoding is>];

But I expect your best course of action is to check that your server is both using and claiming to use UTF-8 encoding or some other Apple iPhone supported encoding.

EDIT

altered code comment.


What helped me was just to change the physical file encoding to UTF-8. My editor had set it to the default, MacRoman, and didn't like letters with accents.

0

精彩评论

暂无评论...
验证码 换一张
取 消