如何在Android Studio中使用Google Cloud Translation API? [英] How to use Google Cloud Translation API in Android Studio?

查看:612
本文介绍了如何在Android Studio中使用Google Cloud Translation API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个用于语言翻译的android应用程序,到目前为止,我已经使用了语音识别器来将语音输入转换为字符串.现在,我想将该字符串翻译成另一种语言,并使用TTS引擎说出翻译后的文本. 我创建了一个单独的translate_test文件供测试使用.我一直在研究,并且知道Android Studio需要API密钥.因此,我创建了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>

推荐答案

我猜您遵循了此处概述的步骤:

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.

Note: Google Cloud Java client libraries do not currently support 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请求时,请将您的密钥作为密钥参数的值传递.例如:

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

为每个字符串使用单独的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': 'Hello world', 'q': 'My name is Jeff', 'target': 'de' }

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

这篇关于如何在Android Studio中使用Google Cloud Translation API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆