AFNetworking 2.0 发送带有字典参数数组的发布请求

Posted

技术标签:

【中文标题】AFNetworking 2.0 发送带有字典参数数组的发布请求【英文标题】:AFNetworking 2.0 Send Post Request with array of dictionary Parameters 【发布时间】:2016-03-07 05:39:15 【问题描述】:

我想在以下 API 中尝试 post 参数,但参数未正确传递,并且收到的响应给出了需要数据的消息。有谁能帮忙解决一下这个问题。

我的网址是

forapp.com/api/getContacts.php?data=[ “姓名”:“abc”,“电话”:“1234567890”,“姓名”:“凯特 铃","电话":"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);

];

【问题讨论】:

usu 喜欢这样。使用 AFNetworking 2.0 ***.com/questions/33767908/… 很容易调用 WS 查看我的回答希望对您有所帮助:- ***.com/a/35497323/3515115 【参考方案1】:

只需添加

第一步

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

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

第二步

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

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

第三步

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

第四步

//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;

第五步

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

];

【讨论】:

我不知道你可以将数组传递给你的请求,我们不知道你的请求的原因,尝试一次 不工作它需要提供相同的响应数据 @Birendra -- 检查更新的答案兄弟,它可以帮助你 @Birendra -- 我一步一步解释,对你有帮助 @Anbu.karthik objectsInCart 是什么?【参考方案2】:

根据这个网址

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

];

【讨论】:

【参考方案3】:

添加以下内容:

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

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

【讨论】:

以上是关于AFNetworking 2.0 发送带有字典参数数组的发布请求的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 2.0 参数字典不适用于 GET 请求

如何使用 AFNetworking 2.0 将包含字典的数组发送到服务器?

Afnetworking 2.0 发布方法错误

AFNetworking 2.0 将 [] 方括号添加到发送到服务器的 json

使用 AFNetworking 发送图像和其他参数

AFNetworking 2.0 和授权 Imgur API