且构网

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

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

更新时间:2023-01-18 12:53:06

我猜你遵循了这里列出的步骤: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.

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': '你好世界','q': '我叫杰夫','目标':'德'}