Requesting Network Data and showing notification in Harmony OS (Java Script)

RChancha
Huawei Developers
Published in
2 min readJul 9, 2021

--

Introduction

Harmony OS is a future-proof distributed operating system open to you as part of the initiatives for all-scenario strategy, adaptable to mobile office, fitness and health, social communication, and media entertainment, etc. Unlike a legacy operating system that runs on a standalone device, Harmony OS is built on a distributed architecture designed based on a set of system capabilities. It can run on a wide range of device forms, including smartphones, tablets, wearables, smart TVs and head units.

In this article, we will make network request using Harmony OS fetch API to get the response. Once we get response in callback, we will parse and show response in notification.

Network request looks like this

Requirement

1) DevEco IDE

2) Wearable simulator

Implementation

First page, index.hml contains button Start, on click of it, we will make network call.

index.css has style defined for the page.

Firstly, we need to import the fetch and notification module in index.js.

On click on start button, we will make network request using fetch API. After receiving response, we will use JSON to parse the result.

After parsing the result, we will show the result in notification

Code snippet of index.js

Tips and Tricks

  1. If you want to support multiple device, you can add device type in config.json

2. HTTPS is supported by default. To support HTTP, you need to add “network” to the config.json file, and set the attribute “usesCleartext” to true.

Conclusion

In this article, we have learnt how easy it is to use fetch API to make network request and parse the response. Once we have the result as parsed response, we are showing it on notification.

Hope you found this story useful and interesting.

Happy coding! 😃 💻

References

  1. Harmony OS JS network request: https://developer.harmonyos.com/en/docs/documentation/doc-references/js-apis-network-data-request-0000000000626077

2. Harmony OS JS notification: https://developer.harmonyos.com/en/docs/documentation/doc-references/js-apis-system-notification-0000000000626084

--

--