AFNetworking QUEUE with pdf, png, mp4 files

2019-08-03 03:13发布

I have a server and i get that response :

 {"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"
    }
]}

i'm using that code to parse the 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];

How can i get all the icon_urls and assign them on a AFnetworking queue for serialized download. Some files are also pdfs or mp4 files in the response so i want all of them packed in an array as requests and one by one to download them.

I've searched for AFClient but could find any source code or an example usage.

2条回答
叛逆
2楼-- · 2019-08-03 04:02

You should follow the MVC architecture in your project. According to your json format follwoings are my suggestions to you.

So first you should have to create a Model name "Product". "Product" should have properties like your json data iconUrl,productId,productName,thumnailURL.

So please take a look below parsing code and fetch the products list first.

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

Now you have full product list.Then you iterate the list and download the image data using following code

NSData *imageData = [NSData dataWithContentsOfURL:
                            [NSURL URLWithString:product.iconUrl]];
查看更多
祖国的老花朵
3楼-- · 2019-08-03 04:03

You should be able to loop through your JSON using that to access each URL and add an asynchronous download for each.

It would be easy to do using a for loop.

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

I'd look at this example on AFNetworking's Github page.

You should be able to setup the download with an AFHTTPRequestOperation documented here.

To set the request up you do something like this:

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

There may be a way to optimize some of this but that's the general idea.

查看更多
登录 后发表回答