使用RestKit发送简单的GET请求

时间:2013-10-28 11:07:32

标签: ios restkit restkit-0.20

我正在尝试使用RestKit发送一个简单的请求,我只对获取JSON感兴趣,

这是我的代码:

NSURL *endpoint = [NSURL URLWithString:@"https://www.ez-point.com/api/v1/"];
    RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:endpoint];
    [objectManager.HTTPClient setAuthorizationHeaderWithToken:@"xxxxxxxxxxx"];
    [objectManager getObjectsAtPath:@"ezpoints" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"It works");
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Request failed");
    }];

我得到的错误是:

    Restkit.network:RKObjectRequestOperation.m:243 GET 
    'https://www.ez-point.com/api/v1/ezpoints' (200 OK / 0 objects) 
    [request=0.0000s mapping=0.0000s total=1.6094s]: Error 
Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x160df470 {NSErrorFailingURLStringKey=https://www.ez-point.com/api/v1/ezpoints, 
    NSLocalizedFailureReason=A 200 response was loaded from the URL 'https://www.ez-point.com/api/v1/ezpoints', which failed to match all (0) response 
    descriptors:, NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=https://www.ez-point.com/api/v1/ezpoints, NSUnderlyingError=0x160a9730 "No mappable object representations were found at the key paths searched."}
        2013-10-28 15:03:40.899 EZ-POINT[1651:c07] Request failed

对这个问题有任何想法吗?

2 个答案:

答案 0 :(得分:5)

如错误所示,RestKit找不到加载响应的任何响应描述符。那是因为你没有设置任何。

如果您只是想加载JSON而不映射它,那么RestKit可能会超过顶部,您应该将您的赌注押在RestKit也使用的AFNetworking上:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

答案 1 :(得分:3)

如果你的兴趣是:

  

仅获取JSON

然后你不应该使用RestKit - 这不是它的用途。相反,使用AFNetworking(它包含在RestKit中,因此您可以使用它们)。

使用正确的工具...