2013-10-29 5 views
0

YouTube에서 검색하려고합니다. 이 코드는 영어 문자와 잘 작동합니다. 그러나 ö, ü, ş와 같은 비 영어 문자를 입력하려고하면 null이 반환됩니다.영어, 영어 이외의 문자로 된 문자 인코딩 문제 -iOS

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

나는

[self.searchField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

답변

0

Hahve 당신이 kStrJsonURL을에는 urlencoding 시도 .. 이런 식으로 URL을 인코딩을 시도했지만 작동하지 않는 이유는 무엇입니까? 이 게시물에 대한 대답을 확인하십시오.

How do I URL encode a string

+0

는 예, 시도 .. 난 그냥이 시도하고 실패를 확인하고 나는이 오류가 무엇입니까 .. – onivi

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]; 
+0

작동하지 않습니다 NSDebugDescription를 = "문자 1 주변의 문자열로 데이터를 변환 할 수 없습니다."; – onivi

+0

내 답변을 수정했습니다. 도움이되는지 확인하십시오. –

+0

불행히도 .. 여전히 같은 오류 .. – onivi

관련 문제