问题
I'm trying to set up a RKRoute to get folder contents from the Dropbox API using RestKit.
The URL to get the contents is https://api.dropbox.com/1/metadata/dropbox/<path>
.
So I set up the response and route like this:
// 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
But then matching of the path fails in [RKPathMather matchesPath:tokenizeQueryStrings:parsedArguments:
because there the number of slashes are checked like this: RKNumberOfSlashesInString(self.patternString) == RKNumberOfSlashesInString(self.rootPath)
The mapping works, when I comment out this checking, but I'm sure it's needed in some other cases.
回答1:
The correct approach is to use 2 different response descriptors and routes. The slashes are important to allow RestKit to properly differentiate between different URLs, paths and patterns. You can use the same mapping so it's 'just' another couple of lines of configuration.
回答2:
I think I found out a cleaner solution for my problem:
I configured my response descriptor and route like this:
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];
Now it works with 1 response descriptor and 1 route. I don't know if this works with other APIs using paths but it does with Dropbox. (The URL for root is now https://api.dropbox.com/1/metadata/dropbox/%2F
)
来源:https://stackoverflow.com/questions/16690770/rkroute-with-as-parameter-doesnt-work