首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFHTTPClient多文件下载

AFHTTPClient多文件下载
EN

Stack Overflow用户
提问于 2012-04-07 18:17:43
回答 1查看 1.8K关注 0票数 0

我正在尝试使用AFNetworking类下载多个文件。我是一个完全的新手,我试图弄清楚AFHTTPClient的工作原理,我已经用操作初始化了AFHTTPClient类的一个对象,但我就是想不出如何开始处理队列中的操作。我也试着查看了文档,但什么也找不到。

下面是我使用的代码:

代码语言:javascript
复制
-(void)downloadFiles:(NSArray *)files {
    if ([files count] > 0) {
        NSMutableArray *operationArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < [files count]; i++) {
            ResourceFile *resourceFile = (ResourceFile*)[files objectAtIndex:i];

                NSString *urlString = [NSString stringWithFormat:@"%@views/ph_session?args=0", BASE_URL];
                NSURL *fileDownloadUrl = [[NSURL alloc] initWithString:urlString];
                NSMutableURLRequest *fileDownloadRequest = [[NSMutableURLRequest alloc] initWithURL:fileDownloadUrl];
                [fileDownloadRequest setValue:[SessionManager getSessionCookieForLogin] forHTTPHeaderField:@"Cookie"];
                void (^successBlock)(NSURLRequest *, NSHTTPURLResponse *, id) = ^(NSURLRequest *request, 
                                                                                  NSHTTPURLResponse *response, 
                                                                                  id JSON) {
                    NSLog(@"Success callback for file %@", resourceFile.filename);
                };
                void (^failureBlock)(NSURLRequest*, NSHTTPURLResponse*, NSError*, id) = ^(NSURLRequest *request, 
                                                                                          NSHTTPURLResponse *response, 
                                                                                          NSError *err_, 
                                                                                          id JSON) {
                    NSLog(@"Failure callback for file %@", resourceFile.filename);
                };
                AFJSONRequestOperation *operation = [AFJSONRequestOperation 
                                                     JSONRequestOperationWithRequest:fileDownloadRequest 
                                                     success:successBlock 
                                                     failure:failureBlock];
                [operationArray addObject:operation];
        }
        if ([operationArray count] > 0) {
            AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];
            void (^progressBlock)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) = ^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
                NSLog(@"%d proccesses completed out of %d", numberOfCompletedOperations, totalNumberOfOperations);
            };
            void (^completionBlock)(NSArray *operations) = ^(NSArray *operations) {
                NSLog(@"All operations completed");
            };

            [requestHandler enqueueBatchOfHTTPRequestOperations:operationArray 
                                                  progressBlock:progressBlock 
                                                completionBlock:completionBlock];
        }
    } else {
        NSLog(@"No file to download");
    }
}

下面是将其数组传递给此方法的ResourceFile类:

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface ResourceFile : NSObject {
    NSInteger fid;
    NSString *filemime;
    NSString *filename;
    NSString *filesize;
    NSString *uri;
    NSString *timestamp;
}

@property NSInteger fid;
@property (retain, nonatomic) NSString *filemime;
@property (retain, nonatomic) NSString *filename;
@property (retain, nonatomic) NSString *filesize;
@property (retain, nonatomic) NSString *uri;
@property (retain, nonatomic) NSString *timestamp;

-(id)initWithData:(id)data;

@end

有人能告诉我我哪里做错了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-14 20:37:14

我发现了问题,我试图使用AFHTTPClient enqueueBatchOfHTTPRequestOperations将AFJSONRequestOperation入队,我应该在AFHTTPClient enqueueBatchOfHTTPRequestOperations入队AFHTTPOperations...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10053598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档