Restkit 0.20基本操作

nim*_*sgb 1 restkit restkit-0.20

我刚刚开始使用RestKit,并且在Rk 0.20正在运行时已经到达,文档和演示是落后的.网上的大多数内容都是0.10卢比,而且0.20版本有很大的变化.

当新版本很快就会启动并运行时,我不想回到早期版本.

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

我很确定我有Mappings等排序,但我的服务需要身份验证,所以我需要将请求中的用户名和密码传递给服务器.

到目前为止我有这个

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];
Run Code Online (Sandbox Code Playgroud)

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

restkit.network:RKObjectRequestOperation.m:296对象请求失败:基础HTTP请求操作失败,错误:错误Domain = org.restkit.RestKit.ErrorDomain Code = -1011"预期状态代码在(200-299),得到401"UserInfo = 0x7884030 {NSLocalizedRecoverySuggestion = {"error":{"code":401,"message":"Unauthorized:Authentication required"}},AFNetworkingOperationFailingURLRequestErrorKey = http://elancovision.umfundi.com>,NSErrorFailingURLKey = http:// elancovision .umfundi.com,NSLocalizedDescription =预期状态代码(200-299),得到401,AFNetworkingOperationFailingURLResponseErrorKey =}

问题当然是我如何在请求中添加用户名和密码.

抱歉,这个菜鸟问题!

JD_*_*JD_ 6

使用基本HTTP身份验证时,应将用户名和密码插入每个请求的HTTP请求授权头字段中.

首先,我建议您使用RKObjectManager来集中请求和映射的配置.http://restkit.org/api/latest/Classes/RKObjectManager.html RKObjectManager可以存储网络参数(通过AFNetworking Library),然后根据用户名/密码,路径,对象映射构建适当的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];
Run Code Online (Sandbox Code Playgroud)

如果身份验证有效,那么查看RESTKit wiki应该会为您提供构建正确映射的下一个提示:https://github.com/RestKit/RestKit/wiki/Object-mapping