AFNetworking 2.0发送带有字典参数数组的发布请求 [英] AFNetworking 2.0 Send Post Request with array of dictionary Parameters

查看:82
本文介绍了AFNetworking 2.0发送带有字典参数数组的发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to try post parameter in the following API but the parameter is not getting passed properly, and the response received gives the message that, data is required. So can anyone please help to sort out this problem.

我的网址是

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

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

那么我如何将这种类型的请求传递给api

so how i can pass this type of request to the 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);

}];

推荐答案

只需添加

第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);

}];

这篇关于AFNetworking 2.0发送带有字典参数数组的发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆