Alex Chihaia
Alex Chihaia

Reputation: 155

How to use Google Translate API v2 with Android

I'm trying to make an Android translator application using Google Translation Api ("google-api-services-translate-v2-rev48.1.22.0.jar).

I managed to get a valid key and I've tested it from a simple Java Project and everything works perfect. But, when I try to use the same code in an Android Application, nothing works.

This is the code from android:

Translate translator = new Translate.Builder (new NetHttpTransport(), GsonFactory.getDefaultInstance(), null)
                .setApplicationName("MyAppName")
                .build();
  try {
            TranslationsListResponse response = getListOfParameters(fromLanguage, toLanguage, textToTranslate).execute();
            StringBuffer sb = new StringBuffer();
            for (TranslationsResource tr : response.getTranslations()) {
                sb.append (tr.getTranslatedText() + " ");
            }
            return sb.toString();
        }
        catch (IOException e) {
            Log.e("ERROR", "Got error while trying to translate");
        }

private Translate.Translations.List getListOfParameters (String fromLanguage, String toLanguage, String textToTranslate) throws IOException {
        Translate.Translations.List list = translator.new Translations().list (Arrays.asList(textToTranslate), toLanguage.toUpperCase());
        list.setKey (TranslatorManager.TRANSLATION_GOOGLE_API_KEY);
        list.setSource (fromLanguage.toUpperCase());

        return list;
    }

I don't know for sure where the problem is. The only thing I get when trying to translate is:

I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false

In android, I've tried withcom.google.api.client.http.javanet.NetHttpTransport() and AndroidHttp.newCompatibleTransport().

In my initial java project, I've used GoogleNetHttpTransport.newTrustedTransport(), but when using it in Android, got me some exceptions:

java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found

Upvotes: 0

Views: 1786

Answers (1)

George
George

Reputation: 1516

The solution requires changing the HTTP Transport used, as Nick writes. The two solutions proposed in the linked thread “[stackoverflow.com/a/39285052/322738 – Rafael Steil][1]” are to some extent equivalent and would work similarly under certain conditions.

The first reply recommends the usage of HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();

whereas the second one: HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();

The [documentation][2] for Class AndroidHttp, in the last paragraph “Method Detail” states that from Android version Gingerbread on, calling “new com.google.api.client.http.javanet.NetHttpTransport();” is recommended.

Method newCompatibleTransport() of the AndroidHttp class returns a new thread-safe HTTP transport instance that is compatible with Android SDKs prior to Gingerbread.

Upvotes: 0

Related Questions