iOS版:在处理HTTP请求的Unicode字符(iOS: handling HTTP reques

2019-07-31 10:32发布

当我的NSLog HTTP请求的响应字符串,它显示为“AA£AAAAAA£Ãâ¬ÃÂãAAAAAA£AA”和的UILabel不同的东西出现,但并不像我期待在日本/中国格式相同。 我使用ASIHTTPRequest和提到这里我已经设置响应编码NSUTF8StringEncoding(服务器使用UTF-8相同),但它并没有帮助。 可能有人请告诉我如何支持Unicode字符在我的应用程序? 谢谢。

- (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]);
}

Answer 1:

我设法解决这个问题。 以下是变化!

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

 request.defaultResponseEncoding = NSUTF8StringEncoding; --> Correct!


文章来源: iOS: handling HTTP request's unicode characters