gpt4 book ai didi

objective-c - Afnetworking post数组

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:39 25 4
gpt4 key购买 nike

我正在尝试使用数组而不是字典作为帖子进行发布。但是我得到一个错误:

Incompatible pointer types sending NSMutableArray to parameter of type NSDictionary

这是代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableArray *parameters = @[@"foo", @"bar"];
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

我怎样才能只发布数组内容?

更新:

这段代码有效,但我更希望得到下面的答案,因为它的清洁器和 af 处理序列化。我猜请求正文不同,但我如何查看正文是什么?

  NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *body = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/resources.json"]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

[request setHTTPMethod:@"POST"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"JSON responseObject: %@ ",responseObject);

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

}];
[op start];

最佳答案

我假设您希望将 foobar 作为没有任何值的参数?如果是这样,你会想做这样的事情

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableArray *parameters = @[@"foo", @"bar"];
NSDictionary *params = [[NSDictionary alloc] initWithObjects:@[[NSNull null], [NSNull null]] forKeys:parameters];
[manager POST:@"http://example.com/resources.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

编辑尝试添加

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

关于objective-c - Afnetworking post数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com