自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS ArkUI之開發(fā)基礎(chǔ)(網(wǎng)絡(luò)請求)

開發(fā) 前端 OpenHarmony
前段時間一直研究ArkUI中的聲明式開發(fā),開發(fā)了一些demo,但都是界面相關(guān)的,相對來說比較基礎(chǔ),也比較簡單。所以研究一下其他的,現(xiàn)在成熟的APP都會有網(wǎng)絡(luò)交互,所以記錄一篇網(wǎng)絡(luò)請求相關(guān)的。

[[438171]]

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

簡介

前段時間一直研究ArkUI中的聲明式開發(fā),開發(fā)了一些demo,但都是界面相關(guān)的,相對來說比較基礎(chǔ),也比較簡單。所以研究一下其他的,現(xiàn)在成熟的APP都會有網(wǎng)絡(luò)交互,所以記錄一篇網(wǎng)絡(luò)請求相關(guān)的。

本項(xiàng)目界面搭建基于ArkUI中TS擴(kuò)展的聲明式開發(fā)范式,關(guān)于語法和概念直接看官網(wǎng)官方文檔地址:

基于TS擴(kuò)展的聲明式開發(fā)范式1、基于TS擴(kuò)展的聲明式開發(fā)范式2

本文介紹開發(fā)基礎(chǔ)知識(網(wǎng)絡(luò)請求):

數(shù)據(jù)接口:聚合免費(fèi)API(天氣預(yù)報(bào))

網(wǎng)絡(luò)請求:ArkUI自帶的網(wǎng)絡(luò)請求

效果演示

#星光計(jì)劃2.0# HarmonyOS ArkUI之開發(fā)基礎(chǔ)(網(wǎng)絡(luò)請求)-鴻蒙HarmonyOS技術(shù)社區(qū)#星光計(jì)劃2.0# HarmonyOS ArkUI之開發(fā)基礎(chǔ)(網(wǎng)絡(luò)請求)-鴻蒙HarmonyOS技術(shù)社區(qū)

開發(fā)步驟

1、聲明網(wǎng)絡(luò)請求權(quán)限

在 entry 下的 config.json 中 module 字段下配置權(quán)限

  1. "reqPermissions": [ 
  2.    { 
  3.       "name""ohos.permission.INTERNET" 
  4.    } 

2、支持http明文請求

默認(rèn)支持https,如果要支持http,在 entry 下的 config.json 中 deviceConfig 字段下配置

  1. "default": { 
  2.   "network": { 
  3.     "cleartextTraffic"true 
  4.   } 

3、創(chuàng)建HttpRequest

  1. // 導(dǎo)入模塊 
  2. import http from '@ohos.net.http'
  3. // 創(chuàng)建HttpRequest對象 
  4. let httpRequest = http.createHttp(); 

4、發(fā)起請求

GET請求(默認(rèn)為GET請求)

  1. // 請求方式:GET 
  2. getRequest() { 
  3.   // 每一個httpRequest對應(yīng)一個http請求任務(wù),不可復(fù)用 
  4.   let httpRequest = http.createHttp() 
  5.   let url = 'http://apis.juhe.cn/simpleWeather/query?key=397c9db4cb0621ad0313123dab416668&city=北京' 
  6.   httpRequest.request(url, (err, data) => { 
  7.     // 處理數(shù)據(jù) 
  8.   }) 

POST請求

目前發(fā)現(xiàn)API的BUG:看官方文檔method可以設(shè)置為字符串,從源碼得知method的類型為:RequestMethod,但是設(shè)置 method: http.RequestMethod.POST 請求數(shù)據(jù)報(bào)錯,設(shè)置成 method: http.POST 可以

  1. // 請求方式:POST 
  2. postRequest() { 
  3.   // 每一個httpRequest對應(yīng)一個http請求任務(wù),不可復(fù)用 
  4.   let httpRequest = http.createHttp() 
  5.   let url = 'http://apis.juhe.cn/simpleWeather/query' 
  6.   httpRequest.request(url, 
  7.     { 
  8.       // 看源碼得知method的類型為:RequestMethod 
  9.       // 但是設(shè)置 method: http.RequestMethod.POST 報(bào)錯 
  10.       // 設(shè)置成 method: http.POST 可以 
  11.       method: http.POST, 
  12.       extraData: { 
  13.         'key''397c9db4cb0621ad0313123dab416668'
  14.         'city''北京' 
  15.       } 
  16.     }, 
  17.     (err, data) => { 
  18.       // 處理數(shù)據(jù) 
  19.     }) 

5、解析數(shù)據(jù)(簡單示例)

(1)網(wǎng)絡(luò)請求到的json字符串

  1.     "name":"梁迪迪"
  2.     "age":"26"
  3.     "sex":"男" 

 (2)創(chuàng)建相應(yīng)的對象

  1. class User { 
  2.   name: string // 姓名 
  3.   age: string // 年齡 
  4.   sex: string // 性別 

(3)解析數(shù)據(jù)

  1. // 請求方式:GET 
  2. getRequest() { 
  3.   // 每一個httpRequest對應(yīng)一個http請求任務(wù),不可復(fù)用 
  4.   let httpRequest = http.createHttp() 
  5.   let url = '' 
  6.   httpRequest.request(url, (err, data) => { 
  7.     // 處理數(shù)據(jù) 
  8.     if (!err) { 
  9.       if (data.responseCode == 200) { 
  10.         // 解析數(shù)據(jù) 
  11.         var userUser = JSON.parse(JSON.stringify(data.result)) 
  12.       } 
  13.     } 
  14.   }) 

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2022-08-25 21:46:51

網(wǎng)絡(luò)通訊應(yīng)用開發(fā)

2023-12-29 08:37:59

2022-02-28 15:44:05

鴻蒙系統(tǒng)鴻蒙API加載網(wǎng)絡(luò)圖片

2022-08-23 16:07:02

ArkUI鴻蒙

2022-08-08 19:46:26

ArkUI鴻蒙

2021-11-23 10:00:55

鴻蒙HarmonyOS應(yīng)用

2011-06-02 10:28:18

2022-06-27 14:12:32

css鴻蒙自定義

2011-06-27 09:02:18

Qt UDP 網(wǎng)絡(luò)

2011-06-02 10:28:21

Rsh

2011-06-02 14:36:25

Netstat

2011-06-02 11:34:31

Nbtstat

2011-06-27 09:47:43

2011-06-02 10:28:15

網(wǎng)絡(luò)命令Arp

2011-06-02 14:36:19

Route

2011-06-02 11:21:59

Tftp

2011-06-02 14:36:22

Runas

2011-06-27 10:28:45

Qt 網(wǎng)絡(luò) TCP

2022-06-30 13:56:05

Rating鴻蒙

2011-06-27 10:15:22

Qt 網(wǎng)絡(luò) TCP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號