Così ho appena passato a RestKit 0.2 e attualmente sto usando il nuovo "HttpClient" che è fondamentalmente un AFHTTPClient. Ho questa riga di codice:JSON Risposta in 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");
}];
chiamate questo post restituire una risposta JSON nella forma: { "api_key": ".... "" username ":" ...."}. Così semplice.
Prima di passare a 0,2, sono stato in grado di ottenere la chiave api_key nella risposta facendo:
[[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
Ma ora, non posso farlo e se faccio un NSLog sulla responseObject, ottengo:
< 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>
E la cosa strana è che se lo faccio:
NSLog(@"%@", operation.responseString);
ho la JSON (in NSString) rivelando.
Quindi due domande:
1) Perché la stampa della responseObject mostrandomi codice esadecimale, e non la risposta in realtà JSON?
2) Perché se eseguo operation.responseString mostra l'oggetto risposta effettivo? C'è un modo per ottenere i dati reali in ResponseObject dopo essere stati analizzati dal JSON?
Grazie!
Anche se questa è una risposta giusta, ma penso che @ phix23 sia meglio, almeno per la rete AFNetworking. ;) – Kjuly