2013-08-20 225 views
6

我试图通过POST将参数发送到我的服务器,它一般工作,但我无法弄清楚如何发送包含数组作为参数的JSON 。以下是我试过的:AFNetworking在发送请求的JSON参数中发送数组

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]]; 
NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]]; 
for(NSDictionary *dict in _cart) 
{ 
    NSObject *object = [dict objectForKey:@"object"]; 
    NSDictionary *objectDict = @{@"product_id": [NSString stringWithFormat:@"%d",[object productID]], 
           @"quantity": [NSString stringWithFormat:@"%d", [[dict objectForKey:@"count"] intValue]], 
           @"store_id": [NSString stringWithFormat:@"%d", [Store getStoreID]], 
           @"price": [NSString stringWithFormat:@"%.2f", [object price]]}; 
    [objectsInCart addObject:objectDict]; 
} 
NSError *error = nil; 
NSString *cartJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart 
                        options:NSJSONWritingPrettyPrinted 
                         error:&error] 
              encoding:NSUTF8StringEncoding]; 

if(error) 
{ 
    NSLog(@"Error serializing cart to JSON: %@", [error description]); 
    return; 
} 

NSDictionary *parameters = @{@"status": @"SUBMITTED", 
          @"orders": cartJSON}; 

NSMutableURLRequest *orderRequest = [httpClient requestWithMethod:@"POST" 
                  path:@"/app/carts" 
                 parameters:parameters]; 

AFJSONRequestOperation *JSONOperation = [[AFJSONRequestOperation alloc] initWithRequest:orderRequest]; 

但是,发送此JSON时出现错误。任何建议都非常感谢!

+0

我不知道服务器期望什么,但通常情况下,JSON中的每个项目都有一个密钥,包括数组。你现在只是发送数组,没有密钥。试试'NSString * cartJSON = @''products':%@“,[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart options:NSJSONWritingPrettyPrinted error:&error] encoding:NSUTF8StringEncoding]; – dirkgroten

+0

如果你看'参数'字典,数组的关键是'@“命令”' – Mason

+0

好吧,我不好,我错过了。你看过通过线路发送的实际数据吗?我非常重视查尔斯代理这样的应用程序,以拦截从我的应用程序到外部服务器的所有流量。 – dirkgroten

回答

9

我没有看到你指定要张贴JSON,所以我打赌你发送表单URL参数编码,这是这样的,根据AFHTTPClient文档:

If a query string pair has a an NSArray for its value, each member of the array will be represented in the format field[]=value1&field[]=value2 . Otherwise, the pair will be formatted as "field=value". String representations of both keys and values are derived using the -description method. The constructed query string does not include the ? character used to delimit the query component.

如果您的服务器确实期待您张贴JSON,在第二行添加此告诉AFNetworking说:

// AFNetworking 1.0 
// httpClient is a subclass of AFHTTPClient 
httpClient.parameterEncoding = AFJSONParameterEncoding; 

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

你会然后删除您的来电NSJSONSerialization,只是把objectsInCartparameters DIC tionary。

甲侧面说明:这是正常子类AFHTTPRequestOperationManagerAFHTTPSessionManager(AFNetworking 2.0)或AFHTTPClient(AFNetworking 1.0),并把这种类型的配置在initWithBaseURL:方法。 (你可能不想为每个请求启动一个新的客户端。)

+0

加耶是的,我发现这一点,应该已经更新了这篇文章。谢谢! – Mason