Good day!

I'm trying to configure api Yandex. Translator inside the application on android. When transferring words in Latin, everything works well, but as soon as I try to translate from Russian into any other language, question marks instead of translation come in response. Translates English to Russian normally.

link = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + myKey + "&text=" + word + "&lang=" + baslang + "-" + learnlang; 

If word is a Cyrillic string, everything is displayed normally in the debug window, but it comes to the server at https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150902T132259Z .448e3d1d80a9ea8e.5584e6a80be79d826cde897c5204d6eacbfae45e & text = ???? & lang = ru-en , that is, instead of the text question marks, respectively, Yandex sends such a "word" untranslated - ????

code itself:

 @Override protected String doInBackground(Void... params) { // получаем данные с внешнего ресурса try { URL url = new URL(link); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return resultJson; } 

How to make the word remain in Cyrillic?

  • Are you sure that the word variable contains valid data? - Vartlok 2:21 pm
  • Show the code. How do you specifically send a request to the site? - Tagir Valeev
  • If this variable contains Cyrillic characters, then everything is bad, if only the Latin script works - Eugene Strelnikov
  • And why did you get your key in the question? To all who wish to use it? :-) - Tagir Valeev
  • the key has already been blocked, they are distributed for free) - Eugene Strelnikov

1 answer 1

You must use URLEncoder.encode :

 import java.net.URLEncoder; link = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + myKey + "&text=" + URLEncoder.encode(word, "UTF-8") + "&lang=" + baslang + "-" + learnlang; 

PS: https://stackoverflow.com/q/607176/4928642

  • UnsupportedEncodingException - Eugene Strelnikov