0

我正在尝试在 youtube 上进行搜索。此代码适用于英文字符。但是当我尝试输入像 ö、ü、ş 这样的非英语字母时,返回 null。

NSString *kStrJsonURL = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=%@&key=API_KEY&format=5&alt=jsonc&callback=?", self.searchField.text];
    NSURL *url = [NSURL URLWithString:kStrJsonURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id getJSON) {
        _JSON = getJSON;
        NSLog(@"%@", _JSON);

    } failure:nil];
    [operation start];

我试图像这样编码 url 但不起作用..

[self.searchField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
4

2 回答 2

0

你试过 URLEncodingkStrJsonURL吗?请参阅此帖子的已接受答案。

如何对字符串进行 URL 编码

于 2013-10-29T15:58:59.383 回答
0

你的代码应该是这样的:

//Added this line
[self.searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
//
    NSString *kStrJsonURL = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=%@&key=API_KEY&format=5&alt=jsonc&callback=?", self.searchField.text];
        NSURL *url = [NSURL URLWithString:[kStrJsonURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id getJSON) {
            _JSON = getJSON;
            NSLog(@"%@", _JSON);

    } failure:nil];
    [operation start];
于 2013-10-29T16:05:25.123 回答