AFNetworking 2.0使用字典参数数组发送发布请求

时间:2016-03-07 05:39:16

标签: ios objective-c json afnetworking-2

我想在以下API中尝试post参数,但参数未正确传递,并且收到的响应会显示需要数据的消息。所以任何人都可以帮助解决这个问题。

我的网址是

  

forapp.com/api/getContacts.php?data= [   {"名称":" ABC""电话":" 1234567890"},{"名称":&# 34;凯特   贝尔","电话":" 9925992599"}]

所以我如何将这种类型的请求传递给api

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name": @"hello",
                             @"phone": @"1234567890"};
    NSLog(@"Dict %@",params);
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];

3 个答案:

答案 0 :(得分:6)

只需添加

<强>步骤1

// create the dictionary of {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} this

NSDictionary *params = @{@"name": @"hello",
                         @"phone": @"1234567890"};

<强>步骤-2

//create the one array of this output [ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

NSMutableArray *arr = [NSMutableArray arrayWithObjects:params,nil];

<强>步骤-3

// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
                                                                                options:NSJSONWritingPrettyPrinted
                                                                                  error:&error]
                                       encoding:NSUTF8StringEncoding];

<强>步骤4

//create the another one dictionary for send the data for like data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

 NSDictionary *pardsams = @{@"data": createJSON};

<强>步骤5

  // finally start your request
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSLog(@"Dict %@",pardsams);
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];

答案 1 :(得分:1)

根据此网址

forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

你应该像这样实施

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name": @"hello",
                             @"phone": @"1234567890"};
    NSLog(@"Dict %@",params);
manager.responseSerializer = [AFJSONResponseSerializer serializer];

manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:@{@"data": @[params]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];

答案 2 :(得分:1)

添加以下内容:

// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager
httpClient.requestSerializer = AFJSONRequestSerializer;

还要检查你的帖子数组不包含任何其他对象。它只包含数组。