带“/”作为参数的RKRoute不起作用

时间:2013-05-22 11:38:14

标签: ios restkit

我正在尝试使用RestKit设置RKRoute以从Dropbox API获取文件夹内容。

获取内容的网址为https://api.dropbox.com/1/metadata/dropbox/<path>

所以我设置了这样的响应和路线:

// objectManagers baseURL is @"https://api.dropbox.com/1/"
RKResponseDescriptor *rootResponse = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping pathPattern:@"metadata/dropbox:path" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[DropboxFolder class] pathPattern:@"metadata/dropbox:path" method:RKRequestMethodGET]];
// dropboxFolder.path is @"/" for root
// dropboxFolder.path is @"/Images" for the "Images"-folder contained in root

但是[RKPathMather matchesPath:tokenizeQueryStrings:parsedArguments:中路径的匹配失败,因为这样会检查斜杠的数量:RKNumberOfSlashesInString(self.patternString) == RKNumberOfSlashesInString(self.rootPath)

当我注释掉这个检查时,映射有效,但我确信在其他一些情况下需要它。

2 个答案:

答案 0 :(得分:1)

正确的方法是使用2个不同的响应描述符和路由。斜杠对于允许RestKit正确区分不同的URL,路径和模式非常重要。您可以使用相同的映射,因此它“只是”另外两行配置。

答案 1 :(得分:1)

我想我找到了一个更清洁的解决方案来解决我的问题:

我配置了我的响应描述符和路由,如下所示:

RKResponseDescriptor *rootResponse = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping pathPattern:@"metadata/dropbox/:path" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKRoute *route = [RKRoute routeWithClass:[DropboxFolder class] pathPattern:@"metadata/dropbox/:path" method:RKRequestMethodGET];
route.shouldEscapePath = YES;
[objectManager.router.routeSet addRoute:route];

现在它可以使用1个响应描述符和1个路由。我不知道这是否适用于使用路径的其他API,但它与Dropbox一起使用。 (root的URL现在是https://api.dropbox.com/1/metadata/dropbox/%2F