在 App Inventor 中編碼 API

  • 瞭解什麼是應用程式介面 (API) 

  • Use an API in a solution to get information

以下是本課的活動:

API

API 代表 應用程式介面.

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
API

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 如何運作。

要使用 API,您必須 

  1. 尋找使用 API 的網站。這裡有一份 清單 按主題列出的可用公開 API。
  2. 閱讀他們的說明文件,瞭解如何使用他們的 API。大多數網站都會提供範例。
  3. 有些網站要求您在註冊 API 金鑰後才能使用其 API。 請注意,有些 API 需要付費才能使用。
  4. 將 API 程式碼納入 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 分鐘

使用 API 顯示水果資訊

  1. 在 App Inventor 圖庫中開啟水果資訊專案的連結。
  2. 將應用程式載入到 App Inventor 中。
  3. 執行應用程式看看它如何運作。
  4. 請查看下面的 JSON 字串範例,以瞭解其外觀。
  5. 現在,應用程式只顯示卡路里資訊。在 CalorieLabel 下方新增第二個標籤,以顯示傳回的其他營養值之一。
開啟圖庫連結

最佳實踐: 有趣的事實,json 代表什麼?"JavaScript Object Notation」。Json 創造於 1999 年,我們每天使用或取得的幾乎所有資料都需要以某種方式使用 json。如果沒有 json,我們擁有的幾乎所有技術都會變成無用。幾乎所有來自雲端的資料都需要 json 來傳遞資料。

向學生提出引導性問題:

您可以在您和朋友之間建立 API 嗎? 

如果您的朋友說 "/hello",您就會回應一句問候。這基本上就是一個 API。一個人請求一些東西 (hello),然後您以您選擇的問候語回應。還有更複雜的例子嗎?/hug、/triviaQuestion、/story、/weekendSummary

導師提示由 AmeriCorps 提供支援。

stylized A, AmeriCorps logo in navy

希望您成功添加了第二個含有 API 資訊的營養標籤!

如果您想查看可能的解決方案,請按一下此按鈕。 

反射

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