How can a translation service be called in an Android app? Can we have an app that can translate multiple languages into another?
How can a translation service be called in an Android app?
Google Translate would seem to be the obvious service to use in an Android App.
There is an unofficial Java client API you could use in your application to make the calls to Google Translate.
If you use this, make sure you give your application the INTERNET
permission so it can make the appropriate network connections.
Can we have an app that can translate multiple languages into another?
It's definitely possible to create an Android app to do this because Google already have made a Google Translate application, as have lots of other people.
This is what i have done:
package com.myapps.translate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class Translator extends Activity {
/** Called when the activity is first created. */
String translatedText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
// TextView tv = new TextView(this);
try {
translatedText = Translate.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
//tv.setText(translatedText);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, translatedText, Toast.LENGTH_SHORT).show();
//setContentView(tv);
}
}
I have nothing appearing in my Toast. Although i have downloaded the jar file , included it in my project and also set the internet permission in my manifest file.Please resolve
Another issue with this API that it throws exception at times. This is what i have got in my log cat window:
02-26 08:15:57.027: WARN/KeyCharacterMap(203): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-26 08:15:59.598: DEBUG/dalvikvm(219): GC freed 1240 objects / 89560 bytes in 163ms
02-26 08:16:00.998: INFO/InetAddress(203): Unknown host ajax.googleapis.com, throwing UnknownHostException
02-26 08:16:00.998: WARN/System.err(203): java.lang.Exception: [google-api-translate-java] Error retrieving translation.
02-26 08:16:00.998: WARN/System.err(203): at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:123)
02-26 08:16:01.007: WARN/System.err(203): at com.google.api.translate.Translate.execute(Translate.java:69)
02-26 08:16:01.007: WARN/System.err(203): at com.myapps.translate.Translator$2.onClick(Translator.java:77)
02-26 08:16:01.007: WARN/System.err(203): at android.view.View.performClick(View.java:2344)
02-26 08:16:01.007: WARN/System.err(203): at android.view.View.onTouchEvent(View.java:4133)
02-26 08:16:01.007: WARN/System.err(203): at android.widget.TextView.onTouchEvent(TextView.java:6510)
02-26 08:16:01.017: WARN/System.err(203): at android.view.View.dispatchTouchEvent(View.java:3672)
02-26 08:16:01.017: WARN/System.err(203): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
02-26 08:16:01.017: WARN/System.err(203): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
02-26 08:16:01.017: WARN/System.err(203): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
02-26 08:16:01.017: WARN/System.err(203): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
02-26 08:16:01.027: WARN/System.err(203): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
02-26 08:16:01.027: WARN/System.err(203): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
02-26 08:16:01.027: WARN/System.err(203): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
02-26 08:16:01.027: WARN/System.err(203): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
02-26 08:16:01.037: WARN/System.err(203): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
02-26 08:16:01.037: WARN/System.err(203): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 08:16:01.037: WARN/System.err(203): at android.os.Looper.loop(Looper.java:123)
02-26 08:16:01.037: WARN/System.err(203): at android.app.ActivityThread.main(ActivityThread.java:4203)
02-26 08:16:01.037: WARN/System.err(203): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 08:16:01.037: WARN/System.err(203): at java.lang.reflect.Method.invoke(Method.java:521)
02-26 08:16:01.048: WARN/System.err(203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-26 08:16:01.048: WARN/System.err(203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-26 08:16:01.048: WARN/System.err(203): at dalvik.system.NativeStart.main(Native Method)
02-26 08:16:01.048: WARN/System.err(203): Caused by: java.net.UnknownHostException: Host is unresolved: ajax.googleapis.com:80
02-26 08:16:01.058: WARN/System.err(203): at java.net.Socket.connect(Socket.java:1002)
02-26 08:16:01.058: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:67)
02-26 08:16:01.058: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
02-26 08:16:01.068: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
02-26 08:16:01.068: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
02-26 08:16:01.068: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
02-26 08:16:01.078: WARN/System.err(203): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1153)
02-26 08:16:01.078: WARN/System.err(203): at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:107)
02-26 08:16:01.087: WARN/System.err(203): ... 23 more
02-26 08:16:05.167: DEBUG/dalvikvm(100): GC freed 387 objects / 19856 bytes in 88ms
Why are we having an Unknown Host Exception here? Any clue?