使用RestKit发布请求

时间:2014-04-25 13:04:36

标签: objective-c restkit-0.20

我是RestKit的新手,我正在敲打将Userdata发布到服务器上

这就是我正在做的事情

#UserData.h        @interface UserProfile:NSObject

@property (nonatomic,strong) NSString *firstName;
@property (nonatomic,strong) NSString *secondName;
@property (nonatomic,strong) NSString *userName;

@end

- (void)configureRestKit
{
    // initialize AFNetworking HTTPClient
    NSURL *baseURL = [NSURL URLWithString:@"http://xx.xxx.xx/myApp"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

    // initialize RestKit
    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[UserData class]];
    [responseMapping addAttributeMappingsFromArray:@[@"firstName", @"secondName", @"userName"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *articleDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];

    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"firstName", @"secondName", @"userName"]];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[UserData class] rootKeyPath:nil method:RKRequestMethodAny];

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://xx.xxx.xx/myApp"]];
    [manager addRequestDescriptor:requestDescriptor];
    [manager addResponseDescriptor:articleDescriptor];

    UserData *user = [UserData new];
    user.firstName = @"firstName";
    user.secondName = @"secondName";
    user.userName = @"userName";

    // POST to create
    // [manager postObject:user path:@"/registration" parameters:nil success:nil failure:nil];

    [manager postObject:user path:@"/userdata/regis" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
       NSLog(@"User is registered: %@", mappingResult.array);

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Error': %@", error);
    }];

}

我收到错误

 Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1700f9e80 {NSUnderlyingError=0x17824e340 "The request timed out."

不确定原因?

这里是restsApi的详细信息 BaseURL:http://xx.xxx.xx/myApp 功能名称:/ regis controName:userdata 参数:UserData objUserData returntype:userDetail obj

0 个答案:

没有答案