且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android Studio中使用Google Cloud Translation API?

更新时间:2023-01-18 13:07:19

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

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' }