错误域= AFNetworkingErrorDomain代码= -1016"请求失败:不可接受的内容类型:video / mp4"

时间:2014-06-25 06:14:57

标签: ios objective-c afnetworking afnetworking-2

我在服务器网址中有视频,然后我使用AFNetworking从网址下载以便稍后播放视频。但是下载时我得到了以下错误

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: video/mp4" UserInfo=0x1e8b6d30 {NSErrorFailingURLKey=http://center.net/projects/AR/Medica/focusvideo.mp4, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1d55c260>, NSLocalizedDescription=Request failed: unacceptable content-type: video/mp4}

下载视频的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
 NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

 if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
 [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 [manager.requestSerializer setValue:@"application/x-www-form-urlencoded"
 forHTTPHeaderField:@"Content-Type"];

 [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"video/mpeg", nil];



 [manager GET:@"http://center.net/projects/AR/Medica/focusvideo.mp4"
 parameters:nil
 success:^(AFHTTPRequestOperation *operation, id responseObject) {
 [operation.responseData writeToFile:[dataPath stringByAppendingPathComponent:@"focusvideo.mp4"] atomically:YES];


 NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]);
 NSLog(@"THE RESPONSE: %@", responseObject);


 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error1) {
 NSLog(@"%@", error1);
 }];

2 个答案:

答案 0 :(得分:1)

在AFURLResponseSerialization.m文件中添加video/mpeg  在可接受的接触类型。

而不是:

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

使用此:

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"video/mpeg", nil];

答案 1 :(得分:0)

如果使用AFNetworking 2.0,您可以使用POST方法,这简化了这一点:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:@"https://mycompany.atlassian.net/rest/auth/latest/session/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这会创建请求,根据Content-Type设置设置requestSerializer,并为您编码JSON。 AFNetworking的一个优点是你可以摆脱手工构建和配置NSURLRequest对象的杂草。