0

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
Alex Chihaia
  • 145
  • 2
  • 4
  • 19
  • See if this other answer helps you: http://stackoverflow.com/a/39285052/322738 – Rafael Steil Jan 08 '17 at 23:23
  • Possible duplicate of [StorageServiceAccountSample application reports "KeyStore JKS implementation not found"](http://stackoverflow.com/questions/18862779/storageserviceaccountsample-application-reports-keystore-jks-implementation-not) – Nick Feb 03 '17 at 20:35
  • OP, is the issue Rafael Steil linked useful in finding a solution? – Nick Feb 03 '17 at 20:36
  • OP, did you attempt to use the suggestions from the thread Rafael linked? It seems the solution is to change the HTTP Transport used. Let us know! – Nick Feb 07 '17 at 19:55

1 Answers1

0

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.

George
  • 1,488
  • 1
  • 10
  • 13
  • Above-mentioned links: [1] http://stackoverflow.com/questions/18862779/storageserviceaccountsample-application-reports-keystore-jks-implementation-not/39285052#39285052 [2] https://developers.google.com/api-client-library/java/google-http-java-client/reference/1.20.0/com/google/api/client/extensions/android/http/AndroidHttp – George May 09 '17 at 18:10