一、介紹
資料來(lái)自官網(wǎng):文檔中心
網(wǎng)絡(luò)管理模塊主要提供以下功能:
- HTTP數(shù)據(jù)請(qǐng)求:通過(guò)HTTP發(fā)起一個(gè)數(shù)據(jù)請(qǐng)求。
- WebSocket連接:使用WebSocket建立服務(wù)器與客戶端的雙向連接。
- Socket連接:通過(guò)Socket進(jìn)行數(shù)據(jù)傳輸。
日常開(kāi)發(fā)中HTTP請(qǐng)求使用會(huì)比較多,主要對(duì)HTTP請(qǐng)求進(jìn)行總結(jié)記錄
二、HTTP請(qǐng)求
場(chǎng)景:應(yīng)用通過(guò)HTTP發(fā)起一個(gè)數(shù)據(jù)請(qǐng)求,支持常見(jiàn)的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
request接口開(kāi)發(fā)步驟?:
- 從@ohos.net.http.d.ts中導(dǎo)入http命名空間。
- 調(diào)用createHttp()方法,創(chuàng)建一個(gè)HttpRequest對(duì)象。
- 調(diào)用該對(duì)象的on()方法,訂閱http響應(yīng)頭事件,此接口會(huì)比request請(qǐng)求先返回??梢愿鶕?jù)業(yè)務(wù)需要訂閱此消息。
- 調(diào)用該對(duì)象的request()方法,傳入http請(qǐng)求的url地址和可選參數(shù),發(fā)起網(wǎng)絡(luò)請(qǐng)求。
- 按照實(shí)際業(yè)務(wù)需要,解析返回結(jié)果。
- 調(diào)用該對(duì)象的off()方法,取消訂閱http響應(yīng)頭事件。
- 當(dāng)該請(qǐng)求使用完畢時(shí),調(diào)用destroy()方法主動(dòng)銷(xiāo)毀。
2.1、開(kāi)發(fā)網(wǎng)絡(luò)權(quán)限。
在model.json5文件中的module模塊下添加如下請(qǐng)求權(quán)限:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
}
],
2.2、使用HTTP請(qǐng)求
2.2.1、定義HTTP請(qǐng)求方法
在ets文件夾下新建model文件夾,在model內(nèi)新建LoginModel文件,用來(lái)處理登錄HTTP請(qǐng)求
model文件夾主要用來(lái)處理數(shù)據(jù)查詢
具體代碼??
import http from '@ohos.net.http'
class LoginModel{
baseUrl:string = 'http://127.0.0.1:8000'
reqLogin(){
return new Promise((resolve,reject) => {
//1.創(chuàng)建http請(qǐng)求
let httpRequest = http.createHttp()
//2.發(fā)送請(qǐng)求
httpRequest.request(
`${this.baseUrl}/saas-api/user/login`,
{
method:http.RequestMethod.POST,
extraData:{'username':'admin','password':'admin'},
header:{
'X-Tenant-ID':'1',
'Content-Type': 'application/json'
},
connectTimeout:10000,
readTimeout:10000
},
).then(resp => {
if(resp.responseCode === 200){
//查詢成功
console.log('http--成功',resp.result)
resolve(JSON.parse(resp.result.toString()))
}else{
console.log('http--失敗',resp.result)
reject('查詢失敗')
}
})
.catch(error => {
console.info('error:'+JSON.stringify(error))
reject('查詢失敗')
})
})
}
}
const loginModel = new LoginModel()
export default loginModel as LoginModel
2.2.2、在頁(yè)面中使用HTTP封裝的請(qǐng)求方法
import LoginModel from '../model/LoginModel'
@Entry
@Component
struct HttpPage {
@State message: string = 'Hello'
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button('http請(qǐng)求')
.onClick(() => {
LoginModel.reqLogin()
})
}
.width('100%')
}
.height('100%')
}
}
在頁(yè)面中點(diǎn)擊 'http請(qǐng)求' 按鈕時(shí),日志會(huì)打印成功的結(jié)果
三、第三方庫(kù)axios?
3.1、下載和安裝ohpm
具體可查看官網(wǎng)指引:文檔中心
3.1.1、下載ohpm工具包,點(diǎn)擊鏈接獲取。
3.1.2、解壓工具包,執(zhí)行初始化命令
3.1.3、將ohpm配置到環(huán)境變量中。
配置好后,打開(kāi)命令窗口輸入 ohpm -v,便能看到版本號(hào)
3.2、下載和安裝axios
3.2.1、下載axios
進(jìn)入項(xiàng)目目錄,輸入下面命令
ohpm install @ohos/axios
安裝成功后,在項(xiàng)目的oh-package.json5文件內(nèi)可以查看到安裝的庫(kù),在oh_modules下也能看到安裝的安裝包
3.2.2、開(kāi)發(fā)網(wǎng)絡(luò)權(quán)限
在model.json5文件中的module模塊下添加如下請(qǐng)求權(quán)限:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
}
],
備注:HTTP請(qǐng)求時(shí)已經(jīng)設(shè)置了請(qǐng)求權(quán)限,此處就不重復(fù)設(shè)置了
推薦:鴻蒙提供的第三方庫(kù)地址OpenHarmony三方庫(kù)中心倉(cāng)
3.3、使用axios
3.3.1、定義axios請(qǐng)求方法
在model內(nèi)新建LoginModel文件LoginModelAxios文件,用來(lái)處理登錄axios請(qǐng)求
import axios from '@ohos/axios'
class LoginModelAxios{
baseUrl:string = 'http://127.0.0.1:8000'
async reqLogin(){
let resp =await axios.post(
`${this.baseUrl}/saas-api/user/login`,
{username:'admin',password:'admin'},
{
headers:{
'X-Tenant-ID':'1'
}
}
)
if(resp.status === 200){
console.log('axios--成功',JSON.stringify(resp.data))
return resp.data
}
//查詢失敗
console.log('axios--失敗',JSON.stringify(resp))
}
}
const loginModelAxios = new LoginModelAxios()
export default loginModelAxios as LoginModelAxios
3.3.2、在頁(yè)面中使用axios封裝的請(qǐng)求方法?
import LoginModel from '../model/LoginModel'
import loginModelAxios from '../model/LoginModelAxios'
@Entry
@Component
struct HttpPage {
@State message: string = 'Hello'
build() {
Row() {
Column({space:8}) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button('http請(qǐng)求')
.onClick(() => {
LoginModel.reqLogin()
})
Button('axios請(qǐng)求')
.onClick(() => {
loginModelAxios.reqLogin()
})
}
.width('100%')
}
.height('100%')
}
}
在頁(yè)面中點(diǎn)擊 'axios請(qǐng)求' 按鈕時(shí),日志會(huì)打印成功的結(jié)果?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-835390.html
最后:???????????????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-835390.html
到了這里,關(guān)于【鴻蒙系統(tǒng)學(xué)習(xí)筆記】網(wǎng)絡(luò)請(qǐng)求的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!