How to use Google Cloud Translation API in Android

2020-07-14 12:42发布

问题:

I am making an android app for language translation and so far I have used voice recognizer intent to get the voice input into a string. Now I want to translate that string into another language and speak the translated text using the TTS Engine. I have created a separate translate_test file just for test use. I have been researching and know that API Key is required in Android Studio. So I have created the API Key and enabled the Google Cloud Translation API. Now I'm trying to import com.google.cloud.translate.Translation in my MainActivity but I'm getting this error:

error

I NEED 10 REPUTATION POINTS TO ALLOW THE IMAGE TO BE SHOWN.So all I can say is that the imported file does not exist.

I need help on how to include the cloud files. I have been researching online but still not able to find a tutorial or any information on how to include the cloud files in Android Studio. I have even read the docs. I need help and will be pleased 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>

回答1:

I guess you followed the steps outlined here: Google Translate API Docs

But this doesn't work on Android as its mentioned in the docs:

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

So in order to get in working on Android, you have to make a REST API call using an HTTP request.

Read this for more info: Authenticating to the Cloud Translation API

Excerpts:

When making any Translation API request, pass your key as the value of a key parameter. For example:

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

Use a separate q parameter for each string. This example HTTP POST request sends two strings for translation:

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

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