gpt4 book ai didi

ios - AFNetworking队列与pdf,png,mp4文件

转载 作者:行者123 更新时间:2023-12-01 17:58:30 25 4
gpt4 key购买 nike

我有一台服务器,并且得到了响应:

 {"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格式,我对您的建议是。
因此,首先您必须创建一个型号名称“Product”。 “产品”应具有json数据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]];

关于ios - AFNetworking队列与pdf,png,mp4文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com