ASIHTTPRequest和西里尔字母

时间:2012-08-24 08:55:55

标签: objective-c xcode networking request asihttprequest

美好的一天, 我正在使用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:。有什么问题?

提前致谢!

1 个答案:

答案 0 :(得分:2)

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

self.getUsersFromSearchRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@&text=%@&count=20", SEARCH_FOR_USER, searchBar.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
相关问题