Restkit 0.20基本操作(Restkit 0.20 basic operation)

2019-08-16 19:21发布

我刚开始接触RestKit和RK 0.20会现场和文档和演示的是落后一步已经抵达而已。 在网络上的大多数东西是RK和0.10中有0.20版本的大的变化。

我不想退回到较早版本时,新的人会很快被启动和运行。

我有一个URL“test.myserver.com”,它返回一个简单的数据报JSON资源 - {“id_user”:“4401”,“datalocation”:“4401”,“国”:“大英”,“数据“: “TESTDATA”, “登录”: “佛瑞德布罗格斯”, “密码”: “579c0cb0ed2dc25db121283f7a98cc71”, “ACCESSLEVEL”: “2”, “时间戳”: “1012”, “datahash”: “2749da29f20ce7a85092323f193adee8”}

我敢肯定我有映射等来分类的,但我的服务要求身份验证,所以我需要在请求到服务器通过用户名和密码。

我有这个迄今为止

NSURL *url = [NSURL URLWithString:@"http://test.myserver.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];

         [objectRequestOperation start];

这似乎与服务器联系,但不可避免地记录以下错误

restkit.network:RKObjectRequestOperation.m:296对象请求失败:基础HTTP请求操作失败,错误:错误域= org.restkit.RestKit.ErrorDomain代码= -1011“预期状态代码在(200-299),得到401”的UserInfo = 0x7884030 {NSLocalizedRecoverySuggestion = { “错误”:{ “代码”:401, “消息”: “未经授权:需要验证”}},AFNetworkingOperationFailingURLRequestErrorKey = HTTP://elancovision.umfundi.com>,NSErrorFailingURLKey = HTTP:// elancovision .umfundi.com在,NSLocalizedDescription =预期状态码(200-299),得到401,AFNetworkingOperationFailingURLResponseErrorKey =}

当然,问题是我怎么添加用户名和密码的请求。

很抱歉的noob问题!

Answer 1:

与基本的HTTP认证,用户名和密码应该被插入到每个请求的HTTP请求授权报头字段。

首先,我建议你使用RKObjectManager集中配置的要求和映射。 http://restkit.org/api/latest/Classes/RKObjectManager.html RKObjectManager可以存储网络参数(通过AFNetworking库),然后建立基于用户名/密码,路径,objectmapping适当的HTTP查询。

适应你的榜样,它会看到这样的:

NSURL* url = [[NSURL alloc]initWithString:@"http://test.myserver.com"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
[objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"];

//NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:@"/yourAPI/yourmethod" parameters:nil];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];

[objectRequestOperation start];

如果认证工作,有看RESTKit维基应该给你的下一个提示,以建立正确的映射: https://github.com/RestKit/RestKit/wiki/Object-mapping



Answer 2:

我这里的解决方案:

// Build a RestKit manager object to look after the restful stuff
RKObjectManager *manager =  [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://test.myserver.com"]];;

// Hash the GUI input password string and pass the username in plain text
NSString *md5PW = [umfundiCommon md5:passwordField.text];           
[manager.HTTPClient setAuthorizationHeaderWithUsername:userField.text password:md5PW];

RKObjectMapping *WebResponse = [RKObjectMapping mappingForClass:[WSObject class]];

        [WebResponse addAttributeMappingsFromDictionary:@{@"id_user":@"id_user", @"datalocation": @"datalocation", @"country":@"country", @"data": @"data", @"login": @"login", @"password": @"password", @"accessLevel": @"accessLevel", @"timestamp": @"timestamp", @"datahash": @"datahash"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:WebResponse pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Add the above response descriptor to the manager
[manager addResponseDescriptor:responseDescriptor];

// the getObject makes the call using the stuff assembled into the manager Object and drops into either the success or the failure routines.
[manager getObject:nil path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
NSLog (@"Server WS call success:");

NSArray *theresults = [result array];              
for (WSObject *item in theresults) {
    NSLog(@"datahash=%@",item.datahash);
    NSLog(@"user_id=%@",item.id_user);
    }
}  failure:^(RKObjectRequestOperation * operation, NSError * error)
    {
    NSLog (@"Server WS call failure: operation: %@ \n\nerror: %@", operation, error);
    }];

........


文章来源: Restkit 0.20 basic operation