Vì vậy, tôi vừa chuyển sang RestKit 0.2 và hiện tôi đang sử dụng "HttpClient" mới về cơ bản là AFHTTPClient. Tôi có dòng mã này:Phản hồi JSON trong postPath AFHTTPClient
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 này trả về trả lời JSON dưới dạng: {"api_key": "...."} "username": "...."}. Đơn giản vậy thôi.
Trước khi chuyển sang 0.2, tôi đã có thể để có được chìa khóa API_KEY trong phản ứng bằng cách thực hiện:
[[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
Nhưng bây giờ, tôi không thể làm điều đó và nếu tôi làm một NSLog trên responseObject, tôi nhận được:
< 7b227265 61736f6e 223a2022 41.504.920 4b657920 666f756e 64222c20 22.617.069 5f6b6579 223a2022 61356661 65323437 66336264 35316164 39396338 63393734 36386438 34636162 36306537 65386331 222c2022 73.756.363 657.373 22 3a207472 75657d>
Và điều kỳ lạ là nếu tôi làm:
NSLog(@"%@", operation.responseString);
tôi có JSON (trong NSString) hiển thị.
Vì vậy, hai câu hỏi:
1) Tại sao in responseObject hiển thị cho tôi mã HEX, và không phải là thực sự JSON phản ứng?
2) Tại sao nếu tôi hoạt động.responseString nó đang hiển thị đối tượng phản hồi thực tế? Có cách nào để có được dữ liệu thực tế trong ResponseObject sau khi được phân tích cú pháp từ JSON?
Cảm ơn!
Mặc dù đây là một câu trả lời đúng, nhưng tôi nghĩ rằng @ phix23 là tốt hơn, ít nhất là cho AFNetworking lib. ;) – Kjuly