这可能与iOS JSON Error: NSDebugDescription=Garbage at end重复,但没有得到答案。
这是我的代码:
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];
然后:
- (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;
}
当JSON响应很大(大约110k左右)时,可以在有wifi连接的情况下完美解析,但在运营商网络上出错:iOS JSON错误:NSDebugDescription =垃圾邮件结束时。在这两种情况下,都会调用didReceiveData两次。
有什么想法吗?谢谢!