RestKit 0.2に切り替えたばかりで、現在、基本的にAFHTTPClientである新しい「HttpClient」を使用しています。私はこのコード行を持っています:
RKObjectManager* objectManager = [RKObjectManager sharedManager];
NSDictionary* params = [[NSDictionary alloc] initWithObjectsAndKeys: login, @"username", password, @"password", nil];
[[objectManager HTTPClient]postPath:@"users/login/?format=json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//reponseObject vs operation.response
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"ERROR");
}];
このPOST呼び出しは、次の形式でJSON応答を返します:{"api_key": "...."、 "username":"...."}。それと同じくらい簡単です。
0.2に切り替える前に、次のようにして、応答でapi_keyキーを取得できました。
[[RKClient sharedClient] post:@"/users/login/?format=json" usingBlock:^(RKRequest *request)
{
request.onDidLoadResponse = ^(RKResponse *response)
{
id parsedResponse = [response parsedBody:NULL];
NSString *apiKey = [parsedResponse valueForKey:@"api_key"];
}
}.....];
http://restkit.org/api/master/Classes/RKResponse.html
しかし、今はそれができません。responseObjectでNSLogを実行すると、次のようになります。
<7b227265 61736f6e 223a2022 41504920 4b657920 666f756e 64222c20 22617069 5f6b6579 223a2022 61356661 65323437 66336264 35316164 39396338 63393734 36386438 34636162 36306537 65386331 222c2022 73756363 65737322 3a207472 75657d>
そして奇妙なことは、私がそうするなら、それです:
NSLog(@"%@", operation.responseString);
JSON(NSString)が表示されています。
したがって、2つの質問:
1)実際のJSON応答ではなく、HEXコードを表示するresponseObjectを出力するのはなぜですか?
2)operation.responseStringを実行すると、実際の応答オブジェクトが表示されるのはなぜですか?JSONから解析された後、ResponseObjectで実際のデータを取得する方法はありますか?