Risposta breve: Assicurarsi di eseguire una versione recente di AFNetworking. Questo è tutto ciò che posso vedere come problema basato sul codice che hai fornito.
Risposta lunga: Ho provato riprodurre il problema che stai descrivendo con le più recenti versioni di AFNetworking e non ho potuto. Ho scavato in AFNetworking per vedere come è fatta la codifica di JSON. AFHTTPClient.m:442 utilizza NSJSONSerialization per codificare le richieste JSON. Sono venuto con il seguente codice per verificare il problema:
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@{@"value" : @YES} options:0 error:&error];
NSLog(@"Resulting JSON:\n\n%@\n", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
uscite:
{"value":true}
Così @YES
dovrebbe farlo. Come nota, assicurati che lo non sia per utilizzare @(YES)
nel tuo codice poiché verrà emesso come 1
anziché true
.
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@{@"value" : @(YES)} options:0 error:&error];
NSLog(@"JSON:%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
uscite:
{"value":1}
Con che ho passato e ho cercato di capire come AFHTTPClient bisogno di essere configurato per inviare una bool come 1
/0
invece di true
/false
e non riuscivano a trovare qualunque. Ecco il mio codice di rete.
AFHTTPClient* httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://<SERVER HERE>"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST" path:@"/" parameters:@{@"value": @YES}];
AFHTTPRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failure");
}];
[jsonOperation start];
fonte
2012-12-24 18:19:06
Poiché @YES è un NSNumber, NSJSONSerialization lo trasforma in 0/1. Non penso ci sia un modo diverso da @ {@ "valore": (yesOrNo? @ "True": @ "false")} o utilizzando una classe di serializzazione diversa. –
@TalBereznitskey Sembra un'implementazione del mio server che si aspetta che un booleano gestisca bene un valore stringa di "true"/"false"! Se dovessi incollarlo come risposta, lo accetterò :) – abyx