導讀
XMLHttpRequest改變了Web應用與服務器交換數(shù)據(jù)的方式,fetch是XMLHttpRequest繼任者,具有更簡潔的語法。本文介紹一下如何使用C語言開發(fā)AWTK-WEB應用程序,并用fetch訪問遠程數(shù)據(jù)。
用?AWTK?Designer?新建一個應用程序
先安裝 AWTK Designer:
https://awtk.zlg.cn/web/index.html
1.?新建應用程序
這里假設應用程序的名稱為 AwtkApplicationCHttp,后面會用到,如果使用其它名稱,后面要做相應修改。
2. 編寫代碼
2.1 用VSCode打開目錄AwtkApplicationCHttp,并打開文件src/pages/home_page.c。
使用其它文本編輯器或 IDE 也可以,推薦使用 VSCode,并開啟 Copilot,將大幅提升開發(fā)效率。
2.2 添加事件處理函數(shù)??梢詤⒖枷旅娴拇a:
// C 函數(shù),用于處理接收到的數(shù)據(jù)
void home_page_on_fetch_data(void* ctx, const char* data, uint32_t data_size) {
widget_t* win = WIDGET(ctx);
conf_doc_t* doc = conf_doc_load_json(data, data_size);
if (doc != NULL) {
const char* city = conf_doc_get_str(doc, "cityInfo.city", "unknown");
const char* shidu = conf_doc_get_str(doc, "data.shidu", "unknown");
const char* wendu = conf_doc_get_str(doc, "data.wendu", "unknown");
const char* quality = conf_doc_get_str(doc, "data.quality", "unknown");
const char* ganmao = conf_doc_get_str(doc, "data.ganmao", "unknown");
double pm25 = conf_doc_get_float(doc, "data.pm25", 0);
widget_set_child_text_utf8(win, "city", city);
widget_set_child_text_utf8(win, "shidu", shidu);
widget_set_child_text_utf8(win, "wendu", wendu);
widget_set_child_text_utf8(win, "quality", quality);
widget_set_child_text_utf8(win, "ganmao", ganmao);
widget_set_child_text_with_double(win, "pm25", "%.0f", pm25);
conf_doc_destroy(doc);
}
printf("Received data (%d bytes): %sn", data_size, data);
}
// C 函數(shù),用于處理錯誤
void home_page_on_fetch_error(void* ctx, const char* error_message) {
printf("Error: %sn", error_message);
}
static ret_t on_update(void* ctx, event_t* e) {
widget_t* win = WIDGET(ctx);
const char* url = "http://localhost:8080/AwtkApplicationCHttp/res/assets/default/raw/data/weather.json";
return_value_if_fail(win != NULL, RET_BAD_PARAMS);
fetch_data(win, "GET", url, home_page_on_fetch_data, home_page_on_fetch_error, NULL);
return RET_OK;
}
/**
* 初始化窗口
*/
ret_t home_page_init(widget_t* win, void* ctx) {
(void)ctx;
return_value_if_fail(win != NULL, RET_BAD_PARAMS);
widget_child_on(win, "update", EVT_CLICK, on_update, win);
return RET_OK;
}
注意:控件的名稱一定要和 home_page.xml 保持一致。
3. 在?AWTK?Designer?中,執(zhí)行“打包”?“編譯”?“模擬運行”
正常情況下可以看到如下界面:
點擊“關閉”按鈕,退出應用程序。
編寫配置文件
具體格式請參考,特殊平臺編譯配置:
https://github.com/zlgopen/awtk/blob/master/docs/build_config.md
這里給出一個例子,可以在此基礎上進行修改,該文件位于:
examples/AwtkApplicationCHttp/build.json
{
"name": "AwtkApplicationCHttp",
"version": "1.0",
"assets": "res/assets",
"vendor": "zlgopen",
"app_name": "org.zlgopen.AwtkApplicationCHttp",
"copyright": "Guangzhou ZHIYUAN Electronics Co.,Ltd.",
"themes":["default"],
"sources": [
"src/*.c",
"src/common/*.c",
"src/pages/*.c",
"src/*.h",
"src/common/*.h",
"src/pages/*.h"
],
"web": {
"app_type": "c",
"assets": "design",
"config": {
"fontScale": "0.8",
"defaultFont": "sans"
}
}
編譯 WEB 應用程序
進入 awtk-web 目錄,不同平臺使用不同的腳本編譯:
- Windows?平臺
./build_win32.sh examples/AwtkApplicationCHttp/build.json release
- Linux?平臺
./build_linux.sh examples/AwtkApplicationCHttp/build.json release
- MacOS?平臺
./build_mac.sh examples/AwtkApplicationCHttp/build.json release
請根據(jù)應用程序所在目錄,修改配置文件的路徑。
運行
1. 正常啟動
./start_web.sh
2. 調(diào)試啟動
start_web_debug.sh
3. 用瀏覽器打開 URL:http://localhost:8080/AwtkApplicationCHttp
點擊?“更新”?按鈕,可以看到數(shù)據(jù)更新。
圖像顯示應用芯片ZMP110X
硬件高集成化
豐富的多媒體特性
廣泛的應用場景
參考價格:45元起