1

我使用 RESTKit 向 Google 搜索 API 发送 HTTP GET:

http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Sulgen

当与 NSURLRequest 一起使用时,此 URL 可以正常工作。

RESTKit 将此字符串转换为

http://maps.googleapis.com/maps/api/geocode/json/?sensor=false&address=Sulgen

提示:在字符串前面插入一个斜杠

我使用 GET 方法来触发请求。这应该如何工作?

编辑:这是导致上述行为的代码:

+ (void)loadGoogleMapsApiLocation_Restkit:(NSString*)searchString
{
/// String definitions
NSString *requestString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Frankfurt"];

NSLog(@"requestString: %@",requestString);

/// Create Mappers
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[GooglePlacemark class]];

[objectMapping addAttributeMappingsFromArray:
                       @[@"formatted_address"]];

/// setup responseDesriptor
NSString *key_path = @"results";

/// use this new Restkit method instead of the deprecated one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
                                             method:RKRequestMethodGET
                                        pathPattern:nil
                                            keyPath:key_path
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

/// Create Manager, connect responseDescriptor and execute
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:requestString]];
[manager addResponseDescriptor:responseDescriptor];

[manager getObject:nil
              path:@""
        parameters:nil
           success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
           {
               NSLog (@"sucess!");
           }

           failure:^(RKObjectRequestOperation *operation, NSError *error)
           {
               NSLog (@"Error: %@", error);
               NSLog (@"Response: %@", operation.HTTPRequestOperation.responseString);
           }
];
}

错误消息如下:

 I restkit.network:RKObjectRequestOperation.m:150 GET 'http://maps.googleapis.com/maps/api/geocode/json/?sensor=false&address=Frankfurt'

E restkit.network:RKObjectRequestOperation.m:547 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0xafaff00 {NSLocalizedRecoverySuggestion=<!DOCTYPE html>

出现 404 消息是因为 Restkit 在请求字符串中插入了反斜杠。如果在浏览器或休息客户端中触发,原始字符串请求可以正常工作。

任何帮助表示赞赏。

4

1 回答 1

2

您需要重新组织您的基本 URL 和路径:

NSString *baseURLString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode"];

NSString *requestPath = @"json";

NSDictionary *parameters = @{@"sensor":@"false",@"address":@"Frankfurt"};

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURLString]];

[manager getObject:nil
              path:requestPath
        parameters:parameters
                   ...
于 2013-11-03T22:24:36.383 回答