AFNetworking QUEUE with pdf, png, mp4 files



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

 {"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 队列上进行序列化下载。有些文件在响应中也是 pdf 或 mp4 文件,所以我希望将它们全部打包成一个数组作为请求并逐个下载它们。

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

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

因此,首先您应该创建一个模型名称"产品"。 "产品"应该具有像你的json数据图标Url,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]];

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

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

最新更新