NSDictionary不是通过HTTP POST发送的

时间:2015-01-28 15:36:26

标签: ios objective-c afnetworking-2

我不知道我做错了什么但是当我尝试发送这个NSDictionary时

NSDictionary *payload = [[NSDictionary alloc]
                         initWithObjects:@[
                                           [[self name] text],
                                           [[self dob] text],
                                           [[self phone] text],
                                           [[self email] text],
                                           [[self address] text],
                                           [[self city] text],
                                           [[self state] text],
                                           [[self zipcode] text],
                                           [NSNumber numberWithBool:hi],
                                           [NSNumber numberWithBool:li],
                                           [NSNumber numberWithBool:di],
                                           [NSNumber numberWithBool:hsa],
                                           [NSNumber numberWithBool:client],
                                           [NSNumber numberWithBool:info]
                                           ]
                         forKeys:@[@"name",
                                   @"dob",
                                   @"phone",
                                   @"email",
                                   @"address",
                                   @"city",
                                   @"state",
                                   @"zipcode",
                                   @"hi",
                                   @"li",
                                   @"di",
                                   @"hsa",
                                   @"client",
                                   @"info"]];

if (online) {
    // enviar el post .

通过POST服务器什么都没有收到,但如果我通过GET这样做,那么没有问题。

    [afFetch POST:baseURL parameters:payload success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[responseObject objectForKey:@"success"] boolValue]) {
            NSLog(@"%@", responseObject);
            [self showSuccessMessage:YES];
        } else {

            [self showSuccessMessage:NO];
        }

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

我想我错过了一些东西,但我无法找到它。有任何想法吗 ?谢谢

1 个答案:

答案 0 :(得分:0)

根据@ ian-macdonald的建议,我使用@{key: value, ...}表示法重构了代码,这解决了问题。这是一个例子:

NSDictionary *payload = @{@"name":[[self name] text],
                          @"dob":[[self dob] text],
                          @"phone":[[self phone] text],
                          @"email":[[self email] text],
                          @"address":[[self address] text],
                          @"city":[[self city] text],
                          @"state":[[self state] text],
                          @"zipcode":[[self zipcode] text],
                          @"hi":[NSNumber numberWithBool:hi],
                          @"li":[NSNumber numberWithBool:li],
                          @"di":[NSNumber numberWithBool:di],
                          @"hsa":[NSNumber numberWithBool:hsa],
                          @"client":[NSNumber numberWithBool:client],
                          @"info":[NSNumber numberWithBool:info]};