如何在 Android Studio 中使用 Google Cloud Translation API?
How to use Google Cloud Translation API in Android Studio?
我正在制作一个用于语言翻译的 android 应用程序,到目前为止,我已经使用语音识别器 Intent 将语音输入转换为字符串。现在我想将该字符串翻译成另一种语言并使用 TTS 引擎说出翻译后的文本。
我创建了一个单独的 translate_test
文件,仅供测试使用。我一直在研究并知道 API 密钥在 Android Studio 中是必需的。所以我创建了 API 密钥并启用了 Google Cloud Translation API。
现在,我正在尝试在我的 MainActivity 中导入 com.google.cloud.translate.Translation
,但出现此错误:
error
我需要 10 个信誉点才能使图像成为 SHOWN.So 我只能说导入的文件不存在。
我需要有关如何包含云文件的帮助。我一直在网上进行研究,但仍然找不到有关如何在 Android Studio 中包含云文件的教程或任何信息。我什至读过 the docs。我需要帮助,如果有人能给我一些简单的步骤,我会很高兴。
这是我的 MainActivity.java 文件:
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);
}
}
这是我的 AndroidManifest.xml 文件:
<?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>
我想您遵循了此处列出的步骤:Google Translate API Docs
但这在 Android 上不起作用,正如文档中提到的那样:
Note: Google Cloud Java client libraries do not currently support Android.
因此,为了开始处理 Android,您必须使用 HTTP 请求进行 REST API 调用。
阅读此内容了解更多信息:Authenticating to the Cloud Translation API
摘录:
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'
}
我正在制作一个用于语言翻译的 android 应用程序,到目前为止,我已经使用语音识别器 Intent 将语音输入转换为字符串。现在我想将该字符串翻译成另一种语言并使用 TTS 引擎说出翻译后的文本。
我创建了一个单独的 translate_test
文件,仅供测试使用。我一直在研究并知道 API 密钥在 Android Studio 中是必需的。所以我创建了 API 密钥并启用了 Google Cloud Translation API。
现在,我正在尝试在我的 MainActivity 中导入 com.google.cloud.translate.Translation
,但出现此错误:
error
我需要 10 个信誉点才能使图像成为 SHOWN.So 我只能说导入的文件不存在。
我需要有关如何包含云文件的帮助。我一直在网上进行研究,但仍然找不到有关如何在 Android Studio 中包含云文件的教程或任何信息。我什至读过 the docs。我需要帮助,如果有人能给我一些简单的步骤,我会很高兴。
这是我的 MainActivity.java 文件:
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);
}
}
这是我的 AndroidManifest.xml 文件:
<?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>
我想您遵循了此处列出的步骤:Google Translate API Docs
但这在 Android 上不起作用,正如文档中提到的那样:
Note: Google Cloud Java client libraries do not currently support Android.
因此,为了开始处理 Android,您必须使用 HTTP 请求进行 REST API 调用。
阅读此内容了解更多信息:Authenticating to the Cloud Translation API
摘录:
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' }