How to use Google Cloud Translation API in Android Studio?

I am making an Android application for translating into English, and so far I have used the voice recognition function to enter voice into a string. Now I want to translate this line into another language and pronounce the translated text using the TTS Engine. I created a separate translate_test file for testing purposes only. I learn and know that an API key is required in Android Studio. So I created an API key and enabled the Google Cloud Translation API. Now I try to import com.google.cloud.translate.Translation into my MainActivity, but I get this error:

error

I NEED 10 REPUTATION POINTS TO ALLOW AN IMAGE THAT MAY BE SHOWN. All I can say is that the imported file does not exist.

I need help on how to include cloud files. I am studying online, but still cannot find a tutorial or any information on how to include cloud files in Android Studio. I even read the docs . I need help and I will be glad if someone can give me some simple steps.

This is my MainActivity.java file:

 package com.example.aman.translate_test; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import com.google.cloud.translate.Translation; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.textView); } } 

This is my AndroidManifest.xml file:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aman.translate_test"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <uses-permission android:name="android.permission.INTERNET"/> <uses-library android:name="com.google.cloud.translate.Translate" /> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/api_key"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

+4
source share
1 answer

I assume that you have completed the following steps: Google API API for Google

But this does not work on Android, as indicated in the docs:

Note. Google Cloud Java client libraries do not currently support Android.

So, in order to work with Android, you have to make a REST API call using an HTTP request.

Read this for more information: Cloud API Authentication

Excerpts:

When executing any translation API request, pass your key as the value of the key parameter. For instance:

POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY

Use a separate q parameter for each row. In this example, the HTTP POST request sends two lines for translation:

POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY

{ 'q': 'Hello world', 'q': 'My name is Jeff', 'target': 'de' }

+3
source

Source: https://habr.com/ru/post/1272548/


All Articles