Encoding issues when requesting offers from Google

I'm trying to get Google Suggestions to work in Greek, but I get an error if the sentences contain characters other than English.

If the request is "magni", for example, the answer will contain the Greek word "μαγνήσιο" as a sentence. Try

Error:

Domain Error = NSCocoaErrorDomain Code = 261 "The file" search "couldnt be opened using Unicode text encoding (UTF-8)." UserInfo = {NSURL = http://suggestqueries.google.com/full/search d = Magni &? client = panel & hl = ash , NSStringEncoding = 4}

Method:

+ (NSArray *)suggestionsForQuery:(NSString *)query { query = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSString *URLString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?q=%@&client=toolbar&hl=%@", query, @"el"]; // el is for greek NSError *error = nil; NSString *XMLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URLString] encoding:NSUTF8StringEncoding error:&error]; if (error) { NSLog(@"Error: %@", error.description); } else { // ... } } 

I tried:

  • Setting the encoding to NSUTF16StringEncoding, but I have some Chinese characters.

  • NSJSONSerialization by setting "toolbar = firefox" to get a JSON response, but I got the same error.

Domain Error = NSCocoaErrorDomain Code = 3840 "Unable to convert data to a string around character 40." UserInfo = {NSDebugDescription = Unable to convert data to a string around character 40.

Any ideas on how to fix it?

+5
source share

Source: https://habr.com/ru/post/1261465/


All Articles