iOS: handling HTTP request's unicode characters

若如初见. 提交于 2019-12-02 17:43:20

问题


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!


来源:https://stackoverflow.com/questions/12316813/ios-handling-http-requests-unicode-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!