【问题标题】:How do download a file with AFNetworking 2.0如何使用 AFNetworking 2.0 下载文件
【发布时间】:2013-11-23 06:10:33
【问题描述】:

我很难筛选搜索结果以了解如何执行此操作,并且在文档中找不到任何具体内容。我只想下载一个文件并存储在 Documents 目录中。该文件的类型为 .plist,但我不需要解析 plist。就像我说的,我只需要将它保存到磁盘。

我是否为此使用 AFHTTPRequestOperationManager?

这是我一直在尝试的一般方法:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer = [AFPropertyListResponseSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager GET:url parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             DLog(@"downloaded plist");
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             DLog(@"JSON DataError: %@", error);
         }];

如果我使用 AFHTTPResponseSerializer 我得到这个:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"

如果我使用 AFPropertyListResponseSerializer 我会得到:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain"

但我什至不确定我使用的是正确的 API。

【问题讨论】:

标签: ios plist afnetworking afnetworking-2


【解决方案1】:

请试试这个例子。 希望这有帮助!

// Step 1: create NSURL with full path to downloading file
// for example try this: NSString *fileUrl = @"https://pbs.twimg.com/profile_images/2331579964/jrqzn4q29vwy4mors75s_400x400.png";
// And create NSURLRequest object with our URL
NSURL *URL = [NSURL URLWithString:fileUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

// Step 2: save downloading file's name
// For example our fileName string is equal to 'jrqzn4q29vwy4mors75s_400x400.png'
NSString *fileName = [URL lastPathComponent];

// Step 3: create AFHTTPRequestOperation object with our request
AFHTTPRequestOperation *downloadRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Step 4: set handling for answer from server and errors with request
[downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            // here we must create NSData object with received data...
            NSData *data = [[NSData alloc] initWithData:responseObject];
            // ... and save this object as file
            // Here 'pathToFile' must be path to directory 'Documents' on your device + filename, of course
            [data writeToFile:pathToFile atomically:YES];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"file downloading error : %@", [error localizedDescription]);
        }];

// Step 5: begin asynchronous download
[downloadRequest start];

其他:What's the best way to find the user's Documents directory on an iPhone?

【讨论】:

  • 也写一些解释。
  • 抱歉我的回答风格不好 :) 现在好点了吗?
  • 你能告诉我如果同一个请求会执行多次怎么办?我们可以在开始下载之前检查是否已经有相同请求的进程在运行吗?
  • 好问题!但是,对不起,我不知道这个问题的答案。但我认为您可以在 'pathToFile' 处检查是否存在文件
  • @BhavikKama,为什么我们不能将下一个请求添加到队列并设置maxConcurrentOperationCount
猜你喜欢
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
相关资源
最近更新 更多