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

时间:2015-10-12 03:24:10

标签: ios afnetworking

我是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中丢失数据?非常感谢你

1 个答案:

答案 0 :(得分:0)

嘛!我发现了这个问题,我又忘了一个必要的参数。问题通过以下方式解决:

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