ASIHTTPRequest and Cyrillic letters

2019-09-19 05:49发布

问题:

Good day, I'm working with ASIHTTPRequest, and I'm facing a problem with cyrillic letters in the request.

I had this request:

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

After this I have requestFinished: and requestFailed: methods:

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

When I enter latin letters into the search text field, everything is going fine, but after I entered cyrillic letters I'm immediately entering requestFailed:. What is wrong?

Thanks in advance!

回答1:

Problem Solved! You must encode the URL string! Like this:

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