所以我只是切換到RestKit 0.2,我目前正在使用新的「HttpClient」,它基本上是一個AFHTTPClient。我有這行代碼:postPath AFHTTPClient中的JSON響應
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」: 「...... 」「 用戶名 」:「 ......」}。就如此容易。
切換到0.2之前,我能夠做得到的響應API_KEY鍵:
[[RKClient sharedClient] post:@"https://stackoverflow.com/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
但現在,我不能這樣做,如果我做的NSLog在responseObject,我得到:
< 7b227265 61736f6e 223a2022 41504920 4b657920 666f756e 64222c20 22617069 5f6b6579 223a2022 61356661 65323437 66336264 35316164 39396338 63393734 36386438 34636162 36306537 65386331 222c2022 73756363 657373 22 3a207472 75657d>
而且奇怪的是,如果我這樣做:
NSLog(@"%@", operation.responseString);
我確實有JSON(在的NSString)顯示出來。
所以兩個問題:
1)爲什麼打印出我的十六進制代碼的responseObject,而不是實際的JSON響應?
2)爲什麼如果我做operation.responseString它顯示實際的響應對象?有沒有辦法從JSON中解析出ResponseObject中的實際數據?
謝謝!
雖然這是一個正確的答案,但我認爲@ phix23更好,至少對於AFNetworking庫。 ;) – Kjuly