在 App Inventor 中编码应用程序接口

  • 了解什么是应用程序编程接口 (API) 

  • Use an API in a solution to get information

这些是本课的活动:

应用程序接口

API 代表 应用程序编程接口.

应用程序接口是一种可以 获取从网站或数据库获取信息的方式。

While cloud storage is usually internal information shared within your solution, APIs allow your solution to access external information from the internet.

云存储
www connected to 3 phones
应用程序接口

Let’s say you are building a solution to help people decide what to wear based on the weather. You could spend lots of time uploading data about the weather into a database, but there are many websites that show the weather. 

A better solution would be just to grab data from a weather website and show it to your users through your solution, by using an API.

下面的视频很好地解释了 API 的工作原理。

要使用应用程序接口,您必须 

  1. 查找使用应用程序接口的网站。下面是一份 列表 按主题列出的可用公共 API。
  2. 阅读它们的文档,了解如何使用 API。大多数网站都会给出示例。
  3. 有些网站要求您在使用其 API 之前注册一个 API 密钥。 请注意,有些 API 的使用需要付费。
  4. 将应用程序接口代码纳入 Thunkable。 

编码示例

下面是一些简单的示例,让您开始在 App Inventor 中使用 API。

Here is a very basic example of using the Google Maps API and the ActivityStarter component to start the Google Maps app from your solution, targeting a specific location. Use this starter template to follow along.

This example tutorial uses an API from OpenWeatherMap.

这个简单的教程将告诉你如何使用 iTunes API在输入艺术家姓名时播放歌曲。

iTunes logo

点击此链接,在 App Inventor 中打开侧边栏教程,然后继续学习。

活动:水果营养应用程序

预计时间30 分钟

使用应用程序接口显示水果信息

  1. 在 App Inventor 图库中打开水果信息项目的链接。
  2. 将应用程序载入 App Inventor。
  3. 运行应用程序,看看它是如何工作的。
  4. 请查看下面的 JSON 字符串示例,了解它的外观。
  5. 现在,应用程序只显示卡路里信息。在 CalorieLabel 下方添加第二个标签,以显示返回的其他营养值之一。
打开图库链接

最佳实践: 有趣的事实,json 代表什么?"JavaScript Object Notation(JavaScript 对象符号)"。json 创建于 1999 年,我们每天使用或获取的几乎所有数据都需要以某种方式使用 json。如果没有 json,我们所拥有的几乎所有技术都将毫无用处。几乎所有来自云端的数据都需要 json 来传递数据。

向学生提出指导性问题:

您能在您和您的朋友之间创建一个 API 吗? 

如果你的朋友说"/hello",你就会回应一句问候。这本质上就是一个 API。一个人提出请求(hello),然后你用自己选择的问候语进行回应。还有更复杂的例子吗?/hug、/triviaQuestion、/story、/weekendSummary

导师提示由 AmeriCorps 提供支持。

stylized A, AmeriCorps logo in navy

希望您成功添加了第二个含有原料药信息的营养标签!

如果您想查看可能的解决方案,请点击此按钮。 

反思

Using APIs in your solution can be a very powerful tool, but it is not easy to implement!

您必须花时间阅读网站的 API 文档,了解如何获取所需的信息。

interconnected network
您认为这个组件对您的数字解决方案有用吗?
您能找到提供所需信息的在线网站吗?

关键术语回顾

  • Application Programming Interface (API) –  a way you can get information from another website or database to use in your solution