2017-08-03 3 views
0

Comment encoder/définir les caractères non anglais UTF-8 (par exemple, caractères chinois 我) à demander dans AFNetworking iOS?Comment passer le caractère non anglais à demander dans AFNetworking iOS?

Mon code:

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:self.httpManager.responseSerializer.acceptableContentTypes]; 
    [contentTypes addObject:@"text/plain"]; 
    [contentTypes addObject:@"text/html"]; 
    [contentTypes addObject:@"application/x-www-form-urlencoded"]; 
    [contentTypes addObject:@"charset=UTF-8"]; 
    [contentTypes addObject:@"application/json"]; 

    self.httpManager.responseSerializer.acceptableContentTypes = 
    [NSSet setWithArray:@[@"text/plain", 
          @"text/html", 
          @"application/x-www-form-urlencoded", 
          @"charset=UTF-8", 
          @"application/json"]]; 

Le backend obtenir quelque chose comme ça

AAEA

au lieu des caractères corrects.

+0

Est-ce pas ce que cela signifie que votre backend décode avec quelque chose d'autre que UTF 8? – Pochi

+0

@Pochi Je ne suis pas sûr puisque je ne peux pas toucher le code backend, mais cela fonctionne pour l'application Android .. – Rendy

+0

@Rendy avez-vous obtenu les mêmes données lorsque vous essayez d'appeler le service dans Postman? – Nirmalsinh

Répondre

0

J'ai le même problème un de mon application. Voici mon code, j'espère qu'il vous aidera.

NSData *data = [strStringWithUnicode dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding]; 
if(goodValue == nil) 
    return strStringWithUnicode; 
return goodValue; 
0

Enfin, c'est la solution:

[self.httpManager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];