0

我正在尝试使用 AFNetworking 将嵌套字典发送到我的服务器。

字典遵循这个非常简单的模式:

NSMutableDictionary *dico1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                             @"value1", @"key1",
                             @"value2", @"key2",
                             @"value3", @"key3",
                             @"value4", @"key4",
                             nil];

NSMutableDictionary *dico2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                             @"value1", @"key1",
                             dico1,     @"key2",
                             @"value3", @"key3",
                             @"value4", @"key4",
                             nil];


当我使用“好老夫妻”NSJSONSerialization/NSURLConnection 将dico2发送到我的服务器时,服务器会收到我所期望的,这意味着(这里是日志跟踪):

parameters =
 { key2: { key2: 'value2', key3: 'value3', key1: 'value1', key4: 'value4' },
   key3: 'value3',
   key1: 'value1',
   key4: 'value4' }

我的服务器是用 javascript 编写的,并检查parameters.key2的存在。在这种情况下,parameters.key2 被定义,因此服务器可以毫无问题地使用它。这是我一直这样做的方式,而且效果很好......

但是现在,我正在尝试使用 AFJSONRequestOperation(使用 [httpClient setParameterEncoding:AFJSONParameterEncoding])发送完全相同的dico2,我收到以下信息:

parameters =
{ key1: 'value1',
  'key2[key1]': 'value1',
  'key2[key2]': 'value2',
  'key2[key3]': 'value3',
  'key2[key4]': 'value4',
  key3: 'value3',
  key4: 'value4' }

我的服务器发送一个错误说parameters.key2是未定义的!

似乎 JSON 数据在 NSJSONSerialization/NSURLConnection 和 AFJSONRequestOperation 之间的编码方式不同。

如何使用 AFJSONRequestOperation 获得与以前相同的编码?
有人可以帮忙吗?

谢谢 !


编辑马特:

这是我使用 AFHTTPClient 的方式:我将 httpClient 分解为我在代码中随处使用的常用方法:

-(NSMutableURLRequest*)formatAFJSONRequest:(NSString*)type command:(NSString*)command parameters:(NSMutableDictionary*)parameters {


NSURL *api_url = [NSURL URLWithString:[NSString stringWithFormat:ADDRESS, API_KEY]];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:api_url];
[httpClient setParameterEncoding:AFJSONParameterEncoding];

NSMutableURLRequest *JSONRequest = [httpClient requestWithMethod:@"GET"
                                                            path:[NSString stringWithFormat:@"%@/%@", type, command]
                                                      parameters:parameters];

JSONRequest.cachePolicy     = CACHE_POLICY_SERVER_REQUEST;
JSONRequest.timeoutInterval = TIMEOUT_SERVER_REQUEST;   

if (DEBUG_1) { NSLog(@"[%@/%@] sent to server (AFN): %@", type, command, parameters); }

return JSONRequest;
}

非常感谢你的帮助!

塞布

4

1 回答 1

1

GET requests don't have HTTP bodies, so parameters are encoded as query strings. If you really need the request to be a GET the work around would be to create a POST request first and then change the method to GET.

This is explained in AFNetworking's docs for AFHTTPClient -parameterEncoding:

The AFHTTPClientParameterEncoding value corresponding to how parameters are encoded into a request body for request methods other than GET, HEAD or DELETE. This is AFFormURLParameterEncoding by default.

Warning: Some nested parameter structures, such as a keyed array of hashes containing inconsistent keys (i.e. @{@"": @[@{@"a" : @(1)}, @{@"b" : @(2)}]}), cannot be unambiguously represented in query strings. It is strongly recommended that an unambiguous encoding, such as AFJSONParameterEncoding, is used when posting complicated or nondeterministic parameter structures.

于 2013-11-11T18:56:48.367 回答