问题描述
我正在制作一个用于语言翻译的 android 应用程序,到目前为止我已经使用语音识别器意图将语音输入输入到字符串中.现在我想将该字符串翻译成另一种语言并使用 TTS 引擎说出翻译后的文本.我创建了一个单独的 translate_test
文件,仅供测试使用.我一直在研究并知道 Android Studio 中需要 API Key.所以我创建了 API 密钥并启用了 Google Cloud Translation API.现在我试图在我的 MainActivity 中导入 com.google.cloud.translate.Translation
但我收到这个错误:
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:
我需要 10 个信誉点才能显示图像.所以我只能说导入的文件不存在.
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.
我需要有关如何包含云文件的帮助.我一直在网上研究,但仍然无法找到有关如何在 Android Studio 中包含云文件的教程或任何信息.我什至阅读了文档.我需要帮助,如果有人能给我一些简单的步骤,我会很高兴.
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.
这是我的 MainActivity.java 文件:
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);
}
}
这是我的 AndroidManifest.xml 文件:
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>
推荐答案
我猜你遵循了这里列出的步骤:Google 翻译 API 文档
I guess you followed the steps outlined here: Google Translate API Docs
但是这在文档中提到的 Android 上不起作用:
But this doesn't work on Android as its mentioned in the docs:
注意:Google Cloud Java 客户端库目前不支持 Android.
因此,为了在 Android 上工作,您必须使用 HTTP 请求进行 REST API 调用.
So in order to get in working on Android, you have to make a REST API call using an HTTP request.
阅读本文了解更多信息:向 Cloud Translation API 进行身份验证
Read this for more info: Authenticating to the Cloud Translation API
摘录:
在发出任何 Translation API 请求时,将您的密钥作为密钥参数的值传递.例如:
POST https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY
为每个字符串使用单独的 q 参数.此示例 HTTP POST 请求发送两个字符串进行翻译:
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': '你好世界','q': '我叫杰夫','目标':'德'}
这篇关于如何在 Android Studio 中使用 Google Cloud Translation API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!