2017-08-03 31 views
0

如何在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

回答

0

我有我的應用程序相同的問題之一。以下是我的代碼,希望它能幫助你。

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

最後,這是解決方案:

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