2

I'm struggling to even get basic stuff working in restkit. I am an experienced developer too which makes it even more frustrating.

I have successfully have it posting a json object to my service and via debugging through the RestKit source I have verified that the desired json has been returned. It returns to the success block but with nothing except an empty object.

Here is my code that sets up the mappings and launches the request.

Note: I have obscured some sensitive passwords/urls.

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"DeviceKey": @"DeviceKey", @"DeviceName": @"DeviceName", @"Password": @"Password", @"Username": @"Username"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:[LoginRequest class]
                                                                               rootKeyPath:nil];


RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
responseMapping.forceCollectionMapping = YES;
[responseMapping addAttributeMappingsFromDictionary:@{@"TotalBalance":@"TotalBalance",@"FirstName":@"FirstName"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                   pathPattern:@"Login"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://api.test.com/account/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
manager.requestSerializationMIMEType = RKMIMETypeJSON;

LoginRequest *request = [[LoginRequest alloc] init];
request.DeviceKey = @"xxxxxxx";
request.DeviceName = @"iPhone";
request.Username = @"mike";
request.Password = @"******";

[manager postObject:request path:@"Login" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSArray * array = [result array];
    NSDictionary * dictionary = [result dictionary];
    NSObject * obj = [result firstObject];
    NSLog(@"We object mapped the response with the following result: %@", result);
    // It ends up in the success callback but all of these are EMPTY :(
} failure:^(RKObjectRequestOperation *operation, NSError *err){
    NSLog(@"We had a problemo");
}];

LogonResponse.h (the .m simply synthisizes them properties)

#import <Foundation/Foundation.h>

@interface LoginResponse : NSObject

@property(nonatomic,assign) double TotalBalance;
@property(nonatomic,retain) NSString * FirstName;

@end

The JSON:

{
    "TotalBalance":22.34,
    "FirstName":"Mike"
    ........ other stuff but this shouldn't matter
}

The console is showing:

2013-06-26 14:03:07.173 Tests.com[1666:907] I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json
2013-06-26 14:03:07.181 Tests.com[1666:3907] T restkit.network:RKHTTPRequestOperation.m:139 POST 'http://api.test.com/account/Login':
request.headers={
    Accept = "application/json";
    "Accept-Language" = "en-GB, en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, ca, hu, vi, en-us;q=0.8";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "Tests.com/2.10.31_dev (iPad; iOS 6.1.3; Scale/1.00)";
}
request.body={"DeviceKey":"xxxxxxx","DeviceName":"iPhone","Password":"******","Username":"mike"}
2013-06-26 14:03:07.297 Tests.com[1666:162f] D restkit.network:RKHTTPRequestOperation.m:191 Received authentication challenge
2013-06-26 14:03:08.016 Tests.com[1666:162f] T restkit.network:RKHTTPRequestOperation.m:156 POST 'http://api.test.com/account/Login' (200):
response.headers={
    "Content-Length" = 1172;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 26 Jun 2013 04:03:04 GMT";
    Server = "Microsoft-IIS/7.5";
    Vary = "Accept-Encoding";
    "X-Powered-By" = "ASP.NET";
}
response.body={"CustomerReference":"50713881","FirstName":"Mike","TotalBalance":22.25,"ErrorInfo":null,"Success":true}
2013-06-26 14:03:08.021 Tests.com[1666:3b13] W restkit.object_mapping:RKMapperOperation.m:76 Adding mapping error: Cannot map a collection of objects onto a non-mutable collection. Unexpected destination object type 'LoginRequest'
(lldb) 

Why is it going on about LoginRequest?? I'm so lost!!

Any help would be very much appreciated.

4

2 回答 2

0

当您使用 RestKit 发出发布请求时,我相信映射器会尝试将响应映射到已发送的相同对象类型。在这种情况下,由于发送了 LoginRequest,它希望将响应映射到 LoginRequest 对象。

对此的解决方案可能是创建一个临时 LoginResponse 对象并将其与包含您的登录请求字段(用户名、密码等)的字典一起发布。这样,RestKit 将能够将响应映射到您创建的临时对象中。

于 2013-06-26T04:12:24.117 回答
0

升级到 v0.20.3 解决了这个问题。

于 2013-06-28T02:20:49.187 回答