问题
I have an iOS application making use of RestKit 0.20-rc1 for RESTful services. I'm trying to perform a GET whereby I am providing multiple query parameters with the same name to retrieve a set of objects of the same type. For example, my URL would look like:
http://mysite.com/rest/myobjects?objID=123&objID=234&objID=345
My web service is able to accept a query like this and return the appropriate objects. My RestKit code on the client looks something like this:
NSDictionary *params = ...
RKObjectManager *objMgr = [RKObjectManager sharedManager];
[objMgr getObjectsAtPath:@"/rest/myobjects" parameters:params success:nil failure:nil];
My problem, is that the parameters must be specified as an NSDictionary
, and I have multiple parameters with the same name. I tried setting the value in the NSDictionary
to an NSArray
containing all of the parameter values, but that did not work.
How do I specify multiple query parameters with the same name in RestKit using this methodology? Is this just not supported in RestKit?
回答1:
Sounds like it is not going to be possible with the RestKit code you have. You may either:
- Change the code to allow NSArrays. RestKit is open source at https://github.com/RestKit
- Ask on the RestKit google group to see how others have worked around this. https://groups.google.com/forum/?fromgroups#!forum/restkit
- Submit a patch to RestKit and wait for a release that supports what you are asking for.
回答2:
You can put the query with the parameters in the getObjectAtPath. Meaning create a string with the parameters like this:
NSString *queryPath = @"getNames?names=bob&names=joe&names=joey";
and then do this (notice that the parameters
object here is nil
):
[[RKObjectManager sharedManager] getObjectsAtPath:queryPath
parameters:nil
success:nil failure:nil];
if you have a problem with the encoding just do this:
queryPath = [queryPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
来源:https://stackoverflow.com/questions/15210694/restkit-0-20-multiple-query-parameters-with-same-name