AFNetworking 3.0 下载 *.m4r 文件
AFNetworking 3.0 download *.m4r file
我有一个旧项目并尝试将其从 AFNetworking 2.* 更新到 AFNetworking 3.0。除了下载 m4r 文件外,一切似乎都正常。旧代码使用了这样的东西:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
AFHTTPRequestOperation* requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFJSONResponseSerializer serializer];
requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:outPath append:NO];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation* operation, id responseObject) {
//
} failure:^(AFHTTPRequestOperation* operation, NSError* error) {
//
}];
[requestOperation setDownloadProgressBlock:downloadProgressBlock];
[requestOperation start];
我不确定,如何将具有输出流逻辑的逻辑移动到 AFNetwroking 3.0,所以现在我离开它并重写了旧代码:
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes: [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray: @[@"video/mp4", @"audio/mpeg"]]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue: @"gzip" forHTTPHeaderField: @"Accept-Encoding"];
[manager GET: URLString parameters: nil progress: downloadProgressBlock success: ^ (NSURLSessionTask* task, id responseObject) {
//
} failure: ^ (NSURLSessionTask* operation, NSError* error) {
//
}];
添加了设置 acceptableContentTypes 的行,因为没有它我会收到错误描述:
"Request failed: unacceptable content-type: video/mp4"
现在,修复此错误后,我得到
"Unable to convert data to a string using the detected encoding. The
data may be corrupt."
我做错了什么?
我已经通过下载任务修复了它。我的代码如下所示:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
NSURLSessionDownloadTask* downloadTask = [manager downloadTaskWithRequest: request progress: downloadProgressBlock destination: ^ NSURL*(NSURL* targetPath, NSURLResponse* response) {
return [[NSURL alloc] initFileURLWithPath: outPath];
} completionHandler: ^ (NSURLResponse* response, NSURL* filePath, NSError* error) {
completion(error == nil, error);
}];
[downloadTask resume];
我有一个旧项目并尝试将其从 AFNetworking 2.* 更新到 AFNetworking 3.0。除了下载 m4r 文件外,一切似乎都正常。旧代码使用了这样的东西:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
AFHTTPRequestOperation* requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFJSONResponseSerializer serializer];
requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:outPath append:NO];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation* operation, id responseObject) {
//
} failure:^(AFHTTPRequestOperation* operation, NSError* error) {
//
}];
[requestOperation setDownloadProgressBlock:downloadProgressBlock];
[requestOperation start];
我不确定,如何将具有输出流逻辑的逻辑移动到 AFNetwroking 3.0,所以现在我离开它并重写了旧代码:
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes: [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray: @[@"video/mp4", @"audio/mpeg"]]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue: @"gzip" forHTTPHeaderField: @"Accept-Encoding"];
[manager GET: URLString parameters: nil progress: downloadProgressBlock success: ^ (NSURLSessionTask* task, id responseObject) {
//
} failure: ^ (NSURLSessionTask* operation, NSError* error) {
//
}];
添加了设置 acceptableContentTypes 的行,因为没有它我会收到错误描述:
"Request failed: unacceptable content-type: video/mp4"
现在,修复此错误后,我得到
"Unable to convert data to a string using the detected encoding. The data may be corrupt."
我做错了什么?
我已经通过下载任务修复了它。我的代码如下所示:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
NSURLSessionDownloadTask* downloadTask = [manager downloadTaskWithRequest: request progress: downloadProgressBlock destination: ^ NSURL*(NSURL* targetPath, NSURLResponse* response) {
return [[NSURL alloc] initFileURLWithPath: outPath];
} completionHandler: ^ (NSURLResponse* response, NSURL* filePath, NSError* error) {
completion(error == nil, error);
}];
[downloadTask resume];