0

美好的一天,我正在使用 ASIHTTPRequest,但我在请求中遇到了西里尔字母的问题。

我有这个要求:

self.getUsersFromSearchRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&text=%@&count=20", SEARCH_FOR_USER, searchBar.text]]];
[self.getUsersFromSearchRequest setDelegate:self];
[self.getUsersFromSearchRequest startAsynchronous];

在此之后我有requestFinished:requestFailed:方法:

    - (void)requestFinished:(ASIHTTPRequest *)request {
    self.dictionary = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONWritingPrettyPrinted error:nil];

    if([request isEqual:self.getUsersFromSearchRequest]) {
        if ([[self.dictionary objectForKey:@"response"]  isKindOfClass:[NSArray class]]) {

            self.resultSearchUser = [self.dictionary objectForKey:@"response"];
            [self.searchDisplayController.searchResultsTableView reloadData];
        }
        self.searchResultsTable.hidden = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Internet Connection Error Alert Title", @"This is the Internet Connection Error Alert Title") message:NSLocalizedString(@"Internet Connection Error Alert Message Text", @"This is the Internet Connection Error Alert Message Text") delegate:nil cancelButtonTitle:NSLocalizedString(@"Internet Connection Error Alert Cancel Button Title", @"This is the Internet Connection Error Alert Cancel Button Title") otherButtonTitles:nil];
    [alertView show];
}

当我在搜索文本字段中输入拉丁字母时,一切正常,但在我输入西里尔字母后,我立即输入requestFailed:。怎么了?

提前致谢!

4

1 回答 1

2

问题解决了!您必须对 URL 字符串进行编码!像这样:

self.getUsersFromSearchRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@&text=%@&count=20", SEARCH_FOR_USER, searchBar.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
于 2012-08-24T08:58:24.070 回答