我正在尝试使用AFNetworking 2.0使用以下代码下载文件
AFHTTPRequestSerializer *serialize = [AFHTTPRequestSerializer new];
[serialize setAuthorizationHeaderFieldWithUsername...// set the auth header with username and password
serialize.timeoutInterval = 200;
NSError *error = nil;
NSMutableURLRequest *request = [serialize requestWithMethod:@"GET" URLString:url parameters:nil error:&error];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSLog(@"success….”);
NSURL *url = [NSURL new];
return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"error: %@", error.localizedDescription);
}];
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
[downloadTask resume];
}
//在此处添加观察者以获取进度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
dispatch_async(dispatch_get_main_queue(), ^{
if ([keyPath isEqualToString:@"fractionCompleted"]) {
NSProgress *progress = (NSProgress *)object;
NSLog(@"Progress… %f total unit count =%lld , completed unit count =%lld", progress.fractionCompleted , progress.totalUnitCount,progress.completedUnitCount);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
});
}
这是来自observeValueForKeyPath
方法的NSLog
SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =0
SampleDownload[5833:149756] Progress… 0.000000 total unit count =-1 , completed unit count =975
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =25149
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =31086
SampleDownload[5833:149767] Progress… 0.000000 total unit count =-1 , completed unit count =53006
SampleDownload[5833:149754] Progress… 0.000000 total unit count =-1 , completed unit count =54377
SampleDownload[5833:149755] Progress… 0.000000 total unit count =-1 , completed unit count =66098
SampleDownload[5833:149768] Progress… 0.000000 total unit count =-1 , completed unit count =93626
SampleDownload[5833:149763] Progress… 0.000000 total unit count =-1 , completed unit count =103805
因此,您可以看到我获得的总单位数为-1。因此,我认为fractionCompleted
为0,我无法更新UI。我犯的错是什么?
我用它作为参考。 How to get download progress in AFNetworking 2.0?
此线程也有类似的讨论,但我无法弄清楚原因。 https://github.com/AFNetworking/AFNetworking/issues/1398
修改
发布回复标题
{
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Headers" = "Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With";
"Access-Control-Allow-Methods" = "GET, POST, PUT, DELETE, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "public, max-age=43200";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 1166764;
"Content-Type" = "application/pdf";
Date = "Wed, 17 Dec 2014 12:17:43 GMT";
Etag = “someEtag”;
Expires = "Thu, 18 Dec 2014 00:17:43 GMT";
"Last-Modified" = "Wed, 17 Dec 2014 11:58:31 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
Vary = "Accept-Encoding";
}
答案 0 :(得分:1)
问题可能是Content-Encoding
的{{1}},这可能导致gzip
方法NSURLSessionDownloadDelegate
返回-1,表示预期的总字节数。您可以尝试在发出请求之前设置didWriteData
来停用gzip
:
Accept-Encoding