RESTKit:BaseURL&路径模式

时间:2014-03-22 14:57:24

标签: json google-maps ios7 restkit restkit-0.20

iOS7& RESTKit 0.20.3

以下是我需要点击以获取Google Maps API响应的网址(我更改了密钥):

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=coffee&sensor=true&key=1234&location=0.000000,0.000000&radius=100.000000

当我在浏览器中输入上面的URL时,我得到一个JSON(200 OK)

input是用户提供的搜索字符串。我已将其硬编码到上面的URL中。

以下是我尝试过的并收到404错误:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/placeautocomplete/json?input=%@&sensor=true&key=1234&location=0.000000,0.000000&radius=100.000000", [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@""
                                                                                           keyPath:@"predictions" statusCodes:statusCodeSet];

我还尝试了以下错误Code=1001 "No response descriptors match the response loaded."

NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKMapping *mapping = [TBSRESTMappingProvider googleAutoCompleteMapping];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=coffee&sensor=true&key=1234&location=0.000000,0.000000&radius=100.000000"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@""
                                                                                       keyPath:@"predictions" statusCodes:statusCodeSet];

RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                                    responseDescriptors:@[responseDescriptor]];
  1. 在不对input sensor location keyradius进行硬编码的情况下提供完整网址的最佳做法是什么?
  2. 如何修复Code=1001 "No response descriptors match the response loaded."

1 个答案:

答案 0 :(得分:2)

您请求的URL路径需要与您提供给响应描述符的路径模式匹配。如果你想按照自己的方式工作,那么将路径模式设置为nil(匹配所有内容)。

最佳做法通常是使用RKObjectManager实例并调用getObjectsAtPath:parameters:success:failure:并传递应附加到请求URL的参数字典(根据基本URL,路径和参数构建)。