2

我将RESTKit用于为我创建的服务。我的用户的 JSON 模型是:

{
    "api_key" : "123456"
    "user" : {
        "username" : "user1",
        "email"    : "me@email.com",
        "name"     : "name1",
        "surname"  : "surname1"
    }
}

api_key如果我想获得一个用户,我需要介绍一下/users/1.json。我在文档中阅读了有关设置应用程序范围的 API 密钥 HTTP 标头的信息。

例子是:

[client setValue:@"123456" forHTTPHeaderField:@"X-RESTKIT-API-KEY"];

这是正确的方法吗?我怎样才能发现我@"X-RESTKIT-API-KEY"的服务?(不是 api_key 值,我搜索字符串以替换@"X-RESTKIT-API-KEY"我的服务)

4

1 回答 1

0

嗯,对于那个示例 GET 请求,我建议您使用 RKClient 对象。我给你举个例子:

#import <RestKit/RestKit.h>
#import "JSONKit.h"

RKClient* client = [RKClient clientWithBaseURL:@"http://yourdomainwhereisapi.com"]; 

在你的应用程序中的任何地方(因为 RKCLient 是单例对象)你可以调用:

// Here you need to define delegate in header (because delegate is self)
// Declare delegate in .h file with <RKRequestDelegate>
[[RKClient sharedClient] get:@"/some/api/call.json" delegate:self];

您必须定义委托方法:

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
      // where you handle response object
      // sample how to make NSDictionary from JSON response
      NSDictionary *deserializedData = [[response bodyAsString] objectFromJSONString];

      // Now you can get anything from this NSDictionary:
      NSLog("%@", [deserializedData objectForKey:@"api_key"];
}

PS 无论如何,你可以看看 RestKit wiki 来查看基本示例:RestKit Wiki tutorial

于 2011-10-13T07:32:24.347 回答