AFNetworking + JSON +进度下载 [英] AFNetworking + JSON + progress download
问题描述
我正在使用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
因此,我认为当与zip文件或图像相反地下载JSON数据时,AFNetworking无法估算要下载的实际大小吗?
通过仔细阅读源代码,进度回调似乎只是传递了缓存的内部NSHTTPURLResponse
对象的expectedContentLength
属性.因此,如果由于某种原因您的服务器未正确发送Content-Length
标头和/或正在执行分块传输编码,则该值是未知的,并且返回值NSURLResponseUnknownLength
(恰好定义为-1) ).
尝试检查应用上下文之外的HTTP请求返回的标头.如果您获得一个具有合理值的Content-Length
标头,则问题可能出在AFNetworking本身.如果不存在,则问题出在服务器上.我从未见过HTTP服务器使用分块传输编码发送JSON响应(大多数情况下,内容大小应该相对较小,并且在发送标头时就知道),但是这样做是在规范之内的. /p>
I'm using AFNetworking and like it very much. I need to get JSON data from my server and it's OK, it works perfectly.
I added the setDownloadProgressBlock but I think it can't work with JSON download: maybe it's not possible to get the estimated amount of bytes to download.
My code:
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];
And my result :
Get 27129 of -1 bytes
Get 127481 of -1 bytes
Get 176699 of -1 bytes
So, I think AFNetworking can't estimate the real size to download when downloading JSON data contrary to a zip file or an image ?
From perusing the source, it seems that the progress callback is just passed the expectedContentLength
property of the cached, internal NSHTTPURLResponse
object. So, if for some reason your server isn't correctly sending the Content-Length
header, and/or is doing chunked transfer encoding, that value is unknown, and the value NSURLResponseUnknownLength
is returned (which happens to be defined as -1).
Try inspecting the headers returned by an HTTP request outside the context of your app. If you get a Content-Length
header with a sane value, the problem likely lies in AFNetworking itself. If it is absent, the problem lies with the server. I've never seen an HTTP server send a JSON response using chunked transfer encoding (most of the time the content size should be relatively small and known at the time the headers are sent), but it's within spec for it to do so.
这篇关于AFNetworking + JSON +进度下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!