通过 AFNetworking 从服务器获取 Json 响应

Posted

技术标签:

【中文标题】通过 AFNetworking 从服务器获取 Json 响应【英文标题】:Get Json response from server by AFNetworking 【发布时间】:2015-10-12 03:24:10 【问题描述】:

我是 AFNetworking 的新手,我正在尝试通过 POST 方法从服务器获取响应,它是成功的,但并不像我预期的那样。事实上,我必须得到一些这样的:


  "status": true,
  "data": 
    "categorylist": 
      "items": [
        
          "CategoryID": "24",
          "CategoryName": "Soup Set",
          "Description": "Soup Set is set for yummy soup with classic and premium soups.",
          "ImageSource": "http://quickorder.gliteservices.com/assets/images/categories/soupset.jpg",
          "CategoryType": "2"
        ,
        
          "CategoryID": "32",
          "CategoryName": "Drinks",
          "Description": "Quench your thirst by ordering one of the drinks",
          "ImageSource": "http://quickorder.gliteservices.com/assets/images/categories/Drinks.jpg",
          "CategoryType": "2"
        
      ],
      "maxPage": "1",
      "maxItem": "9",
      "limitItem": "10"
    
  

但我只得到这个:

data =     
        categorylist =         
            items =             (
            );
            limitItem = 20;
            maxItem = 0;
            maxPage = 0;
        ;
    ;
    status = 1;

那么我错过了什么?我无法检索所有项目,这是我的代码:

-(void)getCategoryList:(void (^)(NSArray *, NSError *))completion
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:[NSString stringWithFormat:@"%@%@",URL_BASE,CATEGORY_LIST_URL] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
        NSLog(@"Response received: %@", [responseObject description]);
        NSMutableArray *arrCategory;
        NSError *error;
        [ParserClass parseCategoryListWithData:responseObject withArray:&arrCategory withError:&error];

        if (error) 
            completion(nil, error);
        else
            completion(arrCategory, nil);
        
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        completion(nil, error);
    ];

你们能帮帮我吗?并解释为什么我从 responseObject 中丢失数据?非常感谢

【问题讨论】:

responseObject 中打印的是哪个值? 【参考方案1】:

好吧!我发现了问题,我忘记了另一个必需的参数。问题通过以下方式解决:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

NSDictionary *params = @@"limit":@"10", @"start":@"0", @"ClientID":@"2";

[manager POST:[NSString stringWithFormat:@"%@%@",URL_BASE,CATEGORY_LIST_URL] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) 
    NSLog(@"Response received: %@", [responseObject description]);
    NSMutableArray *arrCategory;
    NSError *error;
    [ParserClass parseCategoryListWithData:responseObject withArray:&arrCategory withError:&error];

    if (error) 
        completion(nil, error);
    else
        completion(arrCategory, nil);
    
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    completion(nil, error);
];

【讨论】:

以上是关于通过 AFNetworking 从服务器获取 Json 响应的主要内容,如果未能解决你的问题,请参考以下文章

通过 Afnetworking 2.0 从 URL 获取一半大小的 UIImage

如何从服务器获取 AFNetworking 中的完整响应字符串?

从 AFNetworking 获取 JSON 文件或信息

AFNetworking + JSON 并获取服务器状态

iOS10 无法再通过 AFNetworking/NSURLConnection 进行身份验证?

使用 AFNetworking 从 S3 获取公共文件并接收 403