Search code examples
androidtranslateyandexyandex-api

EditText is not displaying translation while Logcat does


I am using Yandex.Translate API to translate a String. It does so successfully as seen in the logcat. But when I set the EditText value (eText) to the translation result, it doesn't parse the data correctly and it shows something like {"code":200,"lang":"en-ru","text":["Он не работает!"]}, the second result instead of the first required result which is "Он не работает!"

2019-06-11 02:36:57.917 14680-                      
14731/com.bahraindiction.goldeneagle.sightling D/Translation Result:: Он 
не работает!
2019-06-11 02:36:57.918 
1468014680/com.bahraindiction.goldeneagle.sightling D/Translation Result:         
{"code":200,"lang":"en-ru","text":["Он не работает!"]}
       TranslatorBackgroundTask translatorBackgroundTask= new TranslatorBackgroundTask(context);
       String translationResult = null; // Returns the translated text as a String
       try {
           translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair).get();
       } catch (ExecutionException e) {
           e.printStackTrace();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       Log.d("Translation Result",translationResult); // Logs the result in Android Monitor
       eText.setText(translationResult);
   }

As seen above, Log.d displays the translated result correctly AND displays the "unparsed" translation, while eText only displays the unparsed result only.


Solution

  • translationResult is in JSON format, Please parse that JSON first and pick the text string and set to eText. You can use gson or similar library to parse JSON.