iOS:RestKit加载对象和发送参数

8

使用GET方法的loadObjectAtResourcePath,请求中不包含我的参数。

例如,如果我发送:

[RKObjectManager objectManagerWithBaseURL:@"http://something/ws"];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/res" delegate:self block:^(RKObjectLoader *loader) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"val", @"param1",
                              nil];
        loader.params = [RKParams paramsWithDictionary:dict];
    }];

最终的 URL 请求不包含 "?param1=val" 部分-为什么?
1个回答

15

几个月后的更新

真正的答案是loader.params创建了HTTP BODY,因此它适用于POST、PUT、DELETE等,但不适用于GET,在GET参数会被附加到URL地址中。

因此,如果您面临相同的GET问题,下面的答案仍然适用,但如果您要发送GET请求,它通常使用将参数附加到查询字符串的方法。

总结一下两者之间的区别。

在HTTP Body中发送参数(即POST、UPDATE、DELETE)

// Convert a NS Dictionary into Params
RKParams *params = [RKParams paramsWithDictionary:optionValues];

// I use sendObject to skip the router. Otherwise it's normally postObject
[[RKObjectManager sharedManager] sendObject:yourObject toResourcePath: yourResourcePath usingBlock:^(RKObjectLoader *loader) {
    loader.method = RKRequestMethodPOST;
    loader.delegate = delegate;
    loader.params = params; // This sets params in the POST body and discards your yourObject mapping

} ];

请注意(针对上面的内容)

在块中设置参数会破坏您可能在yourObject中设置的任何映射,这有点违背了使用对象映射的目的。如果您确实想要使用此方法将额外的参数附加到您的帖子中而不是在对象中,则可以通过Sebastian在loader.params - Extra params中找到修复方法。

将参数作为查询字符串发送(即GET

// Make a NS dictionary and use stringByAppendingQueryParameters
NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" stringByAppendingQueryParameters:shopParams] delegate:objectDelegate];
其余部分仅供参考,我是一个囤积者。

旧回答

我正在为我的项目使用RestKit,并遇到了同样的问题。

我认为RKParams主要用于执行POST请求。由于1)我不知道loader的声明? 2)RKParams不能与Object Manager一起使用,因此我无法完全解密您的代码。

我这样做了。

App Delegate中的加载方法

NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:@"limit",@"30", nil]; 

[[RKClient sharedClient] get:@"/api/v1/shops.json" queryParams:shopParams delegate:self];

委托

- (void)requestDidStartLoad:(RKRequest *)request {
    NSLog(@"RK Request description: %@",[request description]);
}

输出: RK请求描述:<RKRequest: 0x7993db0>,Rails日志显示{"limit"=>"30"}

从Xcode的自动完成中,你可以看到get请求甚至没有使用RKParams,只是一个NSDict。而POST请求则使用了它。

我的目标是在Rails的API方法中附加查询字符串,即?location=singapore&etcetc。为此,RK提供了一个名为appendQueryParamsNSString附加组件RK文档链接,你可以使用它来附加查询参数。

如果你的目标是POST图像等,可以按照上述思路使用RKClient

更新:

如果你只想在对象管理器中附加参数

 NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];

这个已经过时并被标记为弃用。


 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" appendQueryParams:shopParams] delegate:self];

使用这个代替:

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json stringByAppendingQueryParameters:shopParams] delegate:yourLoaderDelegate];

Rails日志: {"location"=>"纬度,经度", "limit"=>"20"}

希望我的答案没有说错什么。

参考这个问题:RestKit GET query parameters


1
最终我也是这样做的。不过,我想使用对象管理器来使用RestKit的映射功能。 - Alon Amir
@daemonsy 非常感谢你的帮助。我已经为此苦苦思索了一段时间。 - drewish
@drewish 谢谢。每当有人觉得这个有用,我就会尽可能更新它,让它更加详尽。 - Damon Aw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接