Ios 带有pdf、png、mp4文件的AFN网络队列

Ios 带有pdf、png、mp4文件的AFN网络队列,ios,json,download,queue,afnetworking,Ios,Json,Download,Queue,Afnetworking,我有一个服务器,我得到了响应: {"products": [ { "product_id": "1170", "name": "zzzz®", "sort_order": 0, "brand": "zzzas", "product_category_id": "1090", "location_ids": [ "1078" ], "icon_url": "http://zzz

我有一个服务器,我得到了响应:

 {"products": [
    {
      "product_id": "1170",
      "name": "zzzz®",
      "sort_order": 0,
      "brand": "zzzas",
      "product_category_id": "1090",
      "location_ids": [
        "1078"
      ],
      "icon_url": "http://zzzzz.com/media/2502/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://zzzzz.com/media/2507/ddddd.png",
      "icon_date": "Wed, 07 Nov 2012 14:03:48 GMT",
      "thumbnail_url": "http://zzzzz.com/media/2596/sssds.png",
      "thumbnail_date": "Wed, 07 Nov 2012 14:04:05 GMT"
    }
]}
我正在使用该代码解析JSON

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];
如何获取所有图标URL并将其分配到AFnetworking队列中进行序列化下载。一些文件在响应中也是PDF或mp4文件,因此我希望所有文件都打包在一个数组中作为请求,并逐个下载它们


我已经搜索了AFClient,但可以找到任何源代码或示例用法。

您应该能够通过JSON循环访问每个URL,并为每个URL添加异步下载

使用for循环很容易做到

for (NSDictionary *dict in [jsonDict objectForKey:@"products"]) {
    NSURL *fileURL = [NSURL URLWithString:[dict valueForKey:@"icon_url"];
    // Some code to start the download.
}
我会在AFNetworking的Github页面上查看

您应该能够使用
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];

也许有一种方法可以优化其中的一些内容,但这是一般的想法。

您应该能够通过JSON循环访问每个URL,并为每个URL添加异步下载

使用for循环很容易做到

for (NSDictionary *dict in [jsonDict objectForKey:@"products"]) {
    NSURL *fileURL = [NSURL URLWithString:[dict valueForKey:@"icon_url"];
    // Some code to start the download.
}
我会在AFNetworking的Github页面上查看

您应该能够使用
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];

也许有一种方法可以优化其中的一些功能,但这是一般的想法。

您应该在项目中遵循MVC体系结构。根据您的json格式,以下是我给您的建议

因此,首先您应该创建一个模型名“Product”。“产品”应具有json数据iconUrl、productId、productName、thumnailURL等属性

因此,请看下面的解析代码并首先获取产品列表

现在您已经有了完整的产品列表。然后您迭代列表并使用以下代码下载图像数据


您应该在项目中遵循MVC体系结构。根据您的json格式,以下是我给您的建议

因此,首先您应该创建一个模型名“Product”。“产品”应具有json数据iconUrl、productId、productName、thumnailURL等属性

因此,请看下面的解析代码并首先获取产品列表

现在您已经有了完整的产品列表。然后您迭代列表并使用以下代码下载图像数据