我新的內部監督辦公室和RestKit並不能弄清楚如何做到以下幾點:RestKit做了一條信息對象和獲取物體響應
1. a simple POST to a webservice
A) with an object payload
B) Have RestKit Auto Serialize the obj c object into Json
2. Receive the Response
A) Parse response and extract only certain fields and load into obj c object
我的設置如下:
的Json從web服務響應:從web服務
{
"user": {
"id":0,
"email":"String",
},
"errorCode": 0
}
響應對象中的obj C:
@interface CreateAccountResponse : NSObject
@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *email;
@property (nonatomic) NSInteger *errorCode;
@end
請求對象OBJ中的C:
@interface CreateAccountRequestModel : NSObject
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, copy) NSString *first_name;
@property (nonatomic, copy) NSString *last_name;
@end
Webservice的預計此JSON的POST請求:
{
"email":"String",
"password":"String",
"first_name":"String",
"last_name":"String"
}
網址是:www.hostname.com/api/AccountManager/createAccount
,而且只接受POST請求。
我曾嘗試:
-(void)SendCreateAccountRequest:(CreateAccountRequestModel*)createActModel
{
NSString *createAccountUrl = @"www.hostname.com/api/AccountManager/createAccount";
NSLog(@"SendCreateAccountRequest");
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass: [CreateAccountResponse class]];
[articleMapping addAttributeMappingsFromDictionary:@{
@"id": @"user.id",
@"email": @"user.email",
@"errorCode": @"errorCode"
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"createAccount" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:createAccountUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
RKLogInfo(@"Successful Response: %@", mappingResult.array);
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
我知道我沒有正確映射從web服務對象的反應,我沒有正確創建POST請求。就像我說我是新的,所以你給更好:)
我是很新的RESTKIT,並努力學習,謝謝爲您提供幫助。 – Aziz