简体   繁体   中英

Get Json response from server by AFNetworking

I'm new to AFNetworking, I'm trying to get response from server by POST method, It's success but not as I expected. In fact, I must got some like this:

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

But I only get this:

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

So what'm I missing? I can't retrieve all items, here is my code:

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

Can you guys help me? And explain why I missing data from responseObject? Thank you very much

Well! I found the issue, I forgot one more required param. The problem is solved by:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM