【发布时间】:2023-03-16 17:45:01
【问题描述】:
我正在尝试从 Web 服务读取一个简单的 json 字符串响应:
假设我有这个映射:
RKObjectMapping* loginRequestMapping = [RKObjectMapping requestMapping];
[loginRequestMapping addAttributeMappingsFromDictionary:@{
@"userName" : @"UserName",
@"password" : @"Password"
}];
RKRequestDescriptor* loginReq = [RKRequestDescriptor requestDescriptorWithMapping:loginRequestMapping objectClass:[LoginRequest class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:loginReq];
我尝试了这个请求:
LoginRequest* loginReq = [LoginRequest new];
loginReq.userName = @"username";
loginReq.password = @"1234567890";
__block NSString* token;
[objectManager postObject:loginReq
path:@"/sendboxapi/api/login"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
token = [mappingResult firstObject];
NSLog(@"RESULT : %@", token);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
原始响应的格式如下:“47c4e389-be2b-466b-b015-c8d5682c4f0a”
我收到此错误:命中错误:Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"
正确的 RKObjectMapping 设置是什么才能从 web 服务读取此字符串?
谢谢
【问题讨论】:
标签: ios objective-c restkit restkit-0.20