使用pdf,png,mp4文件AFNetworking QUEUE

时间:2012-11-20 17:12:51

标签: 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://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];

如何获取所有icon_urls并将其分配到AFnetworking队列以进行序列化下载。一些文件在响应中也是pdfs或mp4文件,所以我希望它们都作为请求打包在一个数组中,并逐个下载它们。

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

2 个答案:

答案 0 :(得分:3)

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

首先,您必须创建一个模型名称“Product”。 “Product”应该包含json data iconUrl,productId,productName,thumnailURL等属性。

因此,请查看以下解析代码并首先获取产品列表。

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

答案 1 :(得分:2)

您应该能够使用它来遍历您的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页面上查看this example

您应该可以使用AFHTTPRequestOperation记录的here来设置下载。

要设置请求,请执行以下操作:

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

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