RestKit 0.20 — Multiple query parameters with same

2019-08-19 05:53发布

问题:

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:

  1. Change the code to allow NSArrays. RestKit is open source at https://github.com/RestKit
  2. Ask on the RestKit google group to see how others have worked around this. https://groups.google.com/forum/?fromgroups#!forum/restkit
  3. 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];



标签: ios rest restkit