我正在使用AFNetworking并非常喜欢它。 我需要从我的服务器获取JSON数据,并且没关系,它完美地工作。AFNetworking + JSON +进度下载
我添加了setDownloadProgressBlock,但我认为它不能与JSON下载一起工作:也许无法得到估计的字节数量下载。
我的代码:
NSMutableURLRequest *request = [[VinocelaHTTPClient sharedClient] requestWithMethod:@"GET" path:@"ws/webapp/services/pull" parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
}];
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Get %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
而且我的结果:
得-1的27129字节
获取127481 -1字节
获取的-1个字节176699
因此,我认为AFNetworking下载JSON数据时不能估算出真正的大小,与zip文件或图像相反?
OK Warrenm。我打印响应头并且内容长度不存在。你是对的。我在我的服务器上使用Rails 3。我会试着找出为什么它不存在,并可能在Rails或类似的东西中强制它。好吧,我在我的Rails配置中添加了这个:config.middleware.use Rack :: ContentLength。但结果很奇怪:“Content-Length”= 2658; =>获取2658字节的176699。我确定JSON的字节数超过2658 ... – 2012-02-09 07:55:12
听起来像内容长度的不匹配可能与完整的和gzip的大小之间的差异有关。不知道这是否有帮助... – mattt 2012-02-13 05:13:08