问题
I need to translate inputed text from one language to another, trying to use this but have problem. This code:
Thread one = new Thread() {
public void run() {
try {
Translator translate = Translator.getInstance();
String text = translate.translate("I am programmer", Language.ENGLISH, Language.PORTUGUESE);
Log.d("TEXT: ",text);
} catch(Exception v) {
Log.d("ERR: ", v.toString());
}
}
};
one.start();
Throws error:
W/System.err: java.io.FileNotFoundException: http://translate.google.com.br/translate_a/t?client=t&text=I%20am%20programmer&hl=en&sl=en&tl=pt&multires=1&prev=btn&ssel=0&tsel=0&sc=1
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
W/System.err: at com.gtranslate.utils.WebUtils.source(WebUtils.java:24)
W/System.err: at com.gtranslate.parsing.ParseTextTranslate.parse(ParseTextTranslate.java:19)
W/System.err: at com.gtranslate.Translator.translate(Translator.java:37)
W/System.err: at org.reikartz.mashkov.recyclerview.MainActivity$1$1.run(MainActivity.java:74)
I don't know, what am i doing wrong.
回答1:
Finally i find out, that http links not valid (in some unknown cause) and desided to use yandex api https://github.com/rmtheis/yandex-translator-java-api
回答2:
The issue is that the language should be installed in your device as well if you need to convert it. The file of Portuguese is not present in your device it is throwing FileNotFoundException
.
回答3:
I think the actual problem is that java-google-translate-text-to-speech is ancient and does not generate valid HTTP requests (the URL from the stack trace gives a 503, which is why you get the FileNotFoundException from the HttpURLConnnection).
You probably want to use the Translate API v2 Client for Java.
来源:https://stackoverflow.com/questions/34412041/using-java-google-translate-text-to-speech-on-android