首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Restkit 0.20基本操作

Restkit 0.20基本操作
EN

Stack Overflow用户
提问于 2013-02-07 04:15:12
回答 2查看 6.3K关注 0票数 1

我刚刚开始使用RestKit,并且已经到达RK0.20即将上线的时候,文档和演示已经落后了一步。网络上的大多数东西都是RK 0.10版本的,而0.20版本有很大的变化。

我不想退回到更早的版本,因为新的版本很快就会启动并运行。

我在网址"test.myserver.com“处有一个JSON资源,它返回一个简单的数据报-{ "id_user":"4401","datalocation":"4401","country":”Britain Britain“,"data":"testdata","login":"Fred Bloggs","password":"579c0cb0ed2dc25db121283f7a98cc71","accessLevel":"2","timestamp":"1012","datahash":”2749da29f20ce7a850923f193adee8“}

我非常确定我已经对Mappings等进行了排序,但是我的服务需要身份验证,所以我需要在请求中向服务器传递用户名和密码。

到目前为止我已经得到了这个

代码语言:javascript
运行
复制
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请求操作失败,错误信息代码=-1011“(200-299)中的预期状态代码,got 401”UserInfo=0x7884030 {“NSLocalizedRecoverySuggestion={”Domain=org.restkit.RestKit.ErrorDomain“:{”代码“:401,"message":”未授权:需要身份验证“} },AFNetworkingOperationFailingURLRequestErrorKey=http://elancovision.umfundi.com>,NSErrorFailingURLKey=http://elancovision.umfundi.com,(200-299)中的NSLocalizedDescription=Expected状态代码,got 401,AFNetworkingOperationFailingURLResponseErrorKey=}

当然,问题是如何将用户名和密码添加到请求中。

对于菜鸟的问题,我很抱歉!

EN

回答 2

Stack Overflow用户

发布于 2013-02-08 03:24:52

对于基本HTTP身份验证,应该将用户名和密码插入到每个请求的HTTP请求授权标头字段中。

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

改编你的例子,它会给出类似这样的结果:

代码语言:javascript
运行
复制
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 wiki就会给出构建正确映射的下一个提示:https://github.com/RestKit/RestKit/wiki/Object-mapping

票数 6
EN

Stack Overflow用户

发布于 2013-02-08 17:54:56

我的解决方案是:

代码语言:javascript
运行
复制
// 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);
    }];

........
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14737936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档