iOS: handling HTTP request's unicode character

2019-03-03 15:10发布

问题:

When I NSLog HTTP requests response string, it appears as "ãÃÂïãÃâ¬ÃÂãÃÂÃâãÃÂ" and something different appears on UILabel but not the same as I expect in Japanese/Chinese format. I am using ASIHTTPRequest and as mentioned here I have set response encoding to NSUTF8StringEncoding(server uses UTF-8 same) but it didn't help. Could someone please tell me how to support unicode character in my App? Thanks.

- (void)getData
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[dataUrl stringByAppendingFormat:@"%@",self.selectedID]]];
    [request setResponseEncoding:NSUTF8StringEncoding];

    SBJSON *parser = [[SBJSON alloc] init];

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithCapacity:3];
    [data setObject:self.username forKey:@"username"];
    [data setObject:self.password forKey:@"password"];

    NSString *dataJSON = [parser stringWithFragment:data error:nil];
    [request appendPostData:[dataJSON dataUsingEncoding:NSUTF8StringEncoding]];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestSuccess:)];
    [request setDidFailSelector:@selector(requestFailed:)];

    [self.queue addOperation: request];
    [self.queue go];
}

- (void)requestSuccess:(ASIHTTPRequest *)request
{
     NSLog(@"success: %@", [request responseString]);
}

回答1:

I managed to fix this. Following is the change!

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[dataUrl stringByAppendingFormat:@"%@",self.selectedID]]];
[request setResponseEncoding:NSUTF8StringEncoding]; -- > Wrong!!!

 request.defaultResponseEncoding = NSUTF8StringEncoding; --> Correct!