2012-06-20 14 views
2

MKNetworkOperation * op = [self operationWithPath:@“thestore/services/storeservice.svc/getfavorites” params:fetchedObjects httpMethod:@“POST”];如何使用MKNetworkKit将NSArray传递到POST

fetechedObjects是一个数组。我可以通过数组循环得到我需要投入POST请求主体的价值观

[ 
{ 
    "id": 9222, 
    "latitude": 19.7897, 
    "longitude": -64.0208 
}, 
{ 
    "id": 7832, 
    "latitude": 79.7713, 
    "longitude": -44.1084 
} 
] 

但我无法弄清楚如何使用MKNetworkOperation

回答

4

的第一件事是尝试设置该操作的postDataEncoding属性为MKNKPostDataEncodingTypeJSON。这应该导致数组被序列化并设置为请求正文。

但是,MKNetworkKit并不真正支持params参数的数组值;它期望一本字典。如果你点击错误的代码路径,它可能会崩溃,因为它期望你提供了一个数组的字典。

+0

是的,它被设置为MKNKPostDataEncodingType JSON。 OK,所以我想我需要扩展网络工具包以使NSArray * fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; MKNetworkOperation * op = [self operationWithPath:@“thestore/services/storeservice.svc/getfavorites” params:fetchedObjects httpMethod:@“POST”]; –

2

这应该可以帮助你。

[op setCustomPostDataEncodingHandler:^NSString *(NSArray *listOfItems) { 

    return [listOfItems description]; 

    } forType:@"text/json"]; 
1

我解决它通过发送阵列(产品)的列表:

NSData *data = [NSJSONSerialization dataWithJSONObject:products options:0 // non-pretty printing error:nil]; ; [params setObject:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:@"list"];