This may be a duplicate of iOS JSON Error: NSDebugDescription=Garbage at end, but that didn't get answered.
Here's my code:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:MY_URL]];
[request:@"POST"];
[request setAllowsCellularAccess:YES]; // not really needed
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Then later:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_jsonData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_jsonData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
When the JSON response is large (>110k or so), it can be parsed perfectly when there's a wifi connection, but gives an error when on a carrier network: iOS JSON Error: NSDebugDescription=Garbage at end. In both cases, didReceiveData is called twice.
Any ideas? Thanks!