如何在AFNetworking iOS中传递非英文字符以请求?

问题描述:

如何在AFNetworking iOS中编码/设置UTF-8非英文字符(例如中文字符我)以请求?如何在AFNetworking iOS中传递非英文字符以请求?

我的代码:

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"]]; 

后端会得到这样的事情

阿拉伯原子能机构

,而不是正确的字符。

+0

这是否意味着你的后端与比UTF-8别的什么解码? – Pochi

+0

@Pochi我不确定,因为我无法触摸后端代码,但它适用于Android应用程序。 – Rendy

+0

@Rendy当您尝试在Postman中调用服务时,您是否获得了相同的数据? – Nirmalsinh

我有我的应用程序相同的问题之一。以下是我的代码,希望它能帮助你。

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

最后,这是解决方案:

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