使用 Thunkable 編寫 API

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

  • Use an API in a solution to get information

以下是本課的活動:

API

API 代表 應用程式介面.

API 是您可以 從從網站或資料庫取得資訊的方式。

While cloud storage is usually internal information shared within your app, 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. 

更好的解決方案是使用 API 從天氣網站擷取資料,並透過您的應用程式顯示給使用者。

這裡有一個很好的視訊解釋 API 如何運作。

要使用 API,您必須 

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

編碼範例

以下是一些教程,可為您提供在 Thunkable 中使用 Web API 的練習。

注意! 在此處列出的一些教程中,Web API 元件顯示在「設計器」視窗中。但是,在當前版本的 Thunkable 中,您將在 Blocks Editor 的 Advanced 下找到 Web API 元件。

按兩下“+”以添加Web API元件。您可以在顯示的屬性視窗中或代碼中新增 URL 和查詢參數。對塊進行編碼應該是相同的。

component window Thunkable showing Web API
此視頻概述了 API 和 Thunkable Web API 元件。

以下是使用 Google 地圖 API 從您自己的應用啟動Google地圖應用,並定位到特定位置。

本範例教程使用來自 OpenWeatherMap.

可作為不同 Web API 示例的整個播放清單進行 Thunkable。

活動:水果營養應用程式

預計時間30 分鐘

使用 API 顯示水果資訊

  1. 在 Thunkable 中打開啟動器專案。
  2. 執行應用程式看看它如何運作。
  3. 製作專案的副本,以便進行編輯。
  4. 請查看下面的 JSON 字串範例,以瞭解其外觀。
  5. 現在,應用程式只顯示卡路里資訊。在 CalorieLabel 下方新增第二個標籤,以顯示傳回的其他營養值之一。
打開 Starter Project

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

向學生提出引導性問題:

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

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

導師提示由 AmeriCorps 提供支援。

stylized A, AmeriCorps logo in navy

以下是 FruityVice為香蕉返回的 JSON 字串。

{ "genus": "Musa", 
"name": "Banana",
"id": 1,
"family": "Musaceae",
"order": "Zingiberales",
"nutritions":
{ "carbohydrates": 22,
"protein": 1,
"fat": 0.2,
"calories": 96,
"sugar": 17.2
}
}

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

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

反射

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

您必須花時間閱讀網站的 API 文件,以瞭解如何取得您需要的資訊。

interconnected network
您認為這對您的數位解決方案來說是個有用的元件嗎?
您能找到提供您所需資訊的線上網站嗎?

關鍵術語的審查

  • 應用程式介面(API) – 一種可從其他網站或資料庫擷取資訊,並將其應用於您的數位解決方案的方法