2016-12-06 107 views
0

我有一箇舊的項目,並嘗試從AFNetworking 2. *更新到AFNetworking 3.0。一切似乎工作正常,除了下載m4r文件。舊代碼中使用這樣的事情: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的行被添加,因爲沒有它,我接收到錯誤與描述:

「請求失敗:不可接受的內容類型:視頻/ MP4」

現在,解決這個錯誤之後,我越來越

「無法將數據轉換成使用檢測到編碼的字符串。該 數據可能已損壞「

我在做什麼錯

回答

0

我已經成功地與下載任務修復我的代碼如下所示:?

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];