所以我试图使用[RKObjectManager postObject:path:parameters:success:failure:]
,但在使用我的登录POST请求时遇到了一些麻烦。出于某种原因,我一直从我的服务器说,所需要的电子邮件和密码参数得到响应回来,即使我通过下面的字典中的参数:无法获得RestKit 0.2到POST参数
NSDictionary *params = @{@"email": @"[email protected], @"password": @"test123!"};
当我注销RKObjectRequestOperation
它没有在请求中显示任何参数。我是否必须在请求中传递一个对象?如果是这样,我会传入什么对象? (之前我只是使用和AFJSONRequestOperation
,但我想更新应用程序以使用RestKit并利用它提供的简单对象映射)。
任何帮助,将不胜感激。
编辑与更多的代码:
我有RKObjectManager的子类,称为UserAuthService,使用RKMIMETYPEJSON作为requestSerializationMIMEType,与下面的请求描述符设置:
// User
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[UserAuthMappingProvider userMapping]
method:RKRequestMethodPOST
pathPattern:@"user/login"
keyPath:@"response.users"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:userResponseDescriptor];
的方法我用实际要求是:
- (void)logUserInWithEmail:(NSString *)email andPassword:(NSString *)password success:(void (^)(UserObject *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure
{
// Request Params
NSDictionary *params = @{@"email": email, @"password": password};
NSLog(@"Params: %@", params);
[self postObject:nil path:@"user/login" parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
if (success)
{
NSArray *userArray = [mappingResult array];
success([userArray firstObject]);
}
}
failure:^(RKObjectRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
if (failure)
{
failure(operation, error);
}
}];
}
在UserAuthMappingProvider的userMapping方法看起来像这样:
+ (RKEntityMapping *)userMapping
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:appDelegate.managedObjectStore];
userMapping.identificationAttributes = @[ @"uuid" ];
[userMapping addAttributeMappingsFromDictionary:@{@"email": @"email",
@"first_name": @"firstName",
@"last_name": @"lastName",
@"is_logged_in": @"isLoggedIn",
@"site_id": @"siteID",
@"user_name": @"username",
@"uuid": @"uuid"}];
return userMapping;
}
和UserObject(每一组中.M到@dynamic):
@interface UserObject : NSManagedObject
@property (strong, nonatomic) NSString *email;
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (assign, nonatomic) BOOL isLoggedIn;
@property (strong, nonatomic) NSNumber *siteID;
@property (strong, nonatomic) NSString *username;
@property (strong, nonatomic) NSString *uuid;
@end
错误我收到的回复是:
Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x8eadbf0 {NSLocalizedRecoverySuggestion={"required_parameters":{"email":"string","password":"string"},"status":"failed","message":"Insufficient information passed. see 'required_parameters'"}
基本上我的目标是获取用户/登录调用的成功响应并将其映射到UserObject。
您需要提供一个对象(与关联的路由)或路径。显示一些代码。使用Charles查看它发送的内容。说明服务器的期望。你设置了什么序列化类型? – Wain
我在上面添加了一些代码,希望有所帮助。 – sfeuerstein
您可以配置restkit网络日志记录以获取更多详细信息:'RKLogConfigureByName(“RestKit/Network *”,RKLogLevelTrace);' – vokilam