gpt4 book ai didi

ios - 无法在 AFNetworking 中获取下载进度

转载 作者:行者123 更新时间:2023-11-29 12:31:34 25 4
gpt4 key购买 nike

我正在尝试使用以下代码使用 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";
}

最佳答案

问题可能是gzipContent-Encoding,会导致NSURLSessionDownloadDelegate方法didWriteData对于预期的总字节数,返回 -1。您可以尝试通过在发出请求之前设置 Accept-Encoding 来禁用 gzip:

[request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];

关于ios - 无法在 AFNetworking 中获取下载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27526731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com