On Iphone as well as android I am getting response codes of 200 meaning that I am getting a valid and good response with no errors, but the json that I am receiving has additional characters. I print out the NSData that I receive and it is 580 in length and the NSString, at least when it fails, will have a larger length and print n+ extra characters on the end of the json. I am not sure why this would be but thought I would see if anyone can see what I would be doing incorrectly.
NSString *post = [NSString stringWithFormat:@"referer=%@&username=%@&password=%@", referer, username, password];
NSData *pData = [post dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES];
NSString *pLength = [NSString stringWithFormat:@"%d", [pData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:self.url];
[request setHTTPMethod:@"POST"];
[request setValue:pLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:pData];
[request setTimeoutInterval:30];
NSHTTPURLResponse *response = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *sData = [NSString stringWithUTF8String:[data bytes]];
I do not receive any errors and every response code that I receive is 200. I have printed out all of these values as well. data length is 580 and sData will vary at times but when it does pass it will be 580 in length, but this can change for different cases. The data will not always be 580 for other users that use the system.
ANSWER:
I solved the problem instead of using
NSString *sData = [NSString stringWithUTF8String:[data bytes]];
You should use
NSString *sData = [[NSString alloc] initWithData: data encoding: NSUTF8String];
This will alloc the correct amount of space based on the NSData object and thus the NSString will never try to access or store more space in that may be behind the last address that should have been accessed.
ANSWER:
I solved the problem instead of using
NSString *sData = [NSString stringWithUTF8String:[data bytes]]; You should use
NSString *sData = [[NSString alloc] initWithData: data encoding: NSUTF8String]; This will alloc the correct amount of space based on the NSData object and thus the NSString will never try to access or store more space in that may be behind the last address that should have been accessed.