AFNetworking参数序列化错误

时间:2014-12-09 15:30:14

标签: ios afnetworking-2

我正在使用这样的AFNetwork:

NSMutableDictionary *parameters = ...;
NSLog(@">>> %@", parameters);

void (^successBlock)(AFHTTPRequestOperation *operation, id responseObject) =  ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"<<< %@", responseObject);
}

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager POST:...
                                       parameters:parameters.copy 
                        constructingBodyWithBlock:...
                                         success:successBlock
                                         failure:...];

parameters字典非常复杂,它包含字典数组,如下所示:

>>> {
    columns = 3;
    rows = 3;
    time = 54;
    invitees = "{(
        {
        email = \"A\";
        firstName = B;
        lastName = C;
        phoneNumber = \"D\";
        },
        {
        email = \"E\";
        firstName = \"F\";
        lastName = G;
        phoneNumber = \"H\";
        }
    )}";
}

但我的服务器和成功块都收到一个字典,其中每个属性都在不同的字典中,如下所示:

 <<<
 {
    error = "-160";
    result =     {
        errorPostReceived =         {
            columns = 3;
            invitees =             (
                                {
                    email = "A";
                },
                                {
                    firstName = B;
                },
                                {
                    lastName = C;
                },
                                {
                    phoneNumber = "D";
                },
                                {
                    email = "E";
                },
                                {
                    firstName = "F";
                },
                                {
                    lastName = G;
                },
                                {
                    phoneNumber = "F";
                }
            );
            rows = 3;
            time = 54;
        };
    };
}

我怀疑AFQueryStringPairsFromKeyAndValue中有AFURLRequestSerialization.m,但我无法确切地知道这里的问题是什么。

1 个答案:

答案 0 :(得分:1)

multipart/form-data请求期望一系列键值对(或文件或你有什么),但&#34;值&#34;与invitees密钥相关联的似乎是description的{​​{1}},这将难以解析(因为它不符合任何既定标准)。

理论上,您可以将该值设为字典的JSON表示,并在服务器端手动NSDictionary(如果您正在使用PHP)。或者你可以将整个请求作为JSON请求,避免json_decode和JSON的奇怪混合(虽然我意识到这可能是一个比你想要考虑的更大的变化。)

但是multipart/form-data规范没有考虑嵌套字典的概念。