使用Arkts功能需要申請(qǐng)ohos.permission.INTERNET權(quán)限。即在module.json5文件中申明網(wǎng)絡(luò)訪問權(quán)限:ohos.permission.INTERNET。如下
{
"module" : {
"requestPermissions":[
{
"name": "ohos.permission.INTERNET"
}
]
}
}
Arkts http數(shù)據(jù)請(qǐng)求功能主要由http模塊提供。具體接口說明如下表。
接口名 |
功能描述 |
createHttp() |
創(chuàng)建一個(gè)http請(qǐng)求。 |
request() |
根據(jù)URL地址,發(fā)起HTTP網(wǎng)絡(luò)請(qǐng)求。 |
destroy() |
中斷請(qǐng)求任務(wù)。 |
on(type: 'headersReceive') |
訂閱HTTP Response Header 事件。 |
off(type: 'headersReceive') |
取消訂閱HTTP Response Header 事件。 |
- ?首先需要引入http模塊
import http from '@ohos.net.http';
- 創(chuàng)建一個(gè)HTTP請(qǐng)求,返回一個(gè)HttpRequest對(duì)象
// 每一個(gè)httpRequest對(duì)應(yīng)一個(gè)http請(qǐng)求任務(wù),不可復(fù)用
let httpRequest = http.createHttp();
-
(可選)訂閱HTTP響應(yīng)頭。
-
根據(jù)URL地址,發(fā)起HTTP網(wǎng)絡(luò)請(qǐng)求。
-
(可選)處理HTTP響應(yīng)頭和HTTP網(wǎng)絡(luò)請(qǐng)求的返回結(jié)果。
httpRequest.request('接口地址',{
method: http.RequestMethod.POST, // 可選,默認(rèn)為http.RequestMethod.GET
// 開發(fā)者根據(jù)自身業(yè)務(wù)需要添加header字段
header: {
'Content-Type': 'application/json'
},
// 當(dāng)使用POST請(qǐng)求時(shí)此字段用于傳遞內(nèi)容
extraData: {
"data": "data to send",
},
connectTimeout: 60000, // 可選,默認(rèn)為60s
readTimeout: 60000, // 可選,默認(rèn)為60s
}, (err,data) => {
if (!err) {
// data.result為http響應(yīng)內(nèi)容,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
console.info('Result:' + data.result);
console.info('code:' + data.responseCode);
// data.header為http響應(yīng)頭,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + data.cookies); // 8+
} else {
console.info('error:' + JSON.stringify(err));
// 該請(qǐng)求不再使用,調(diào)用destroy方法主動(dòng)銷毀。
httpRequest.destroy();
}
})
案例:獲取詩詞接公開API接口
/*
* 發(fā)起http請(qǐng)求
* */
// 1:導(dǎo)入http模塊
import http from '@ohos.net.http'
@Entry
@Component
struct HttpReq {
@State poem: string = '把酒祝東風(fēng)'
@State from:string = '柳宗元'
aboutToAppear(){
setInterval(() => {
// 2. 常見http請(qǐng)求對(duì)象
let httpReq = http.createHttp()
// 3. 發(fā)起請(qǐng)求
httpReq.request('https://api.apiopen.top/api/sentences',
{
method:http.RequestMethod.GET,
},
(err,data) => {
// 4. 處理結(jié)果
if (!err) {
this.poem = JSON.parse(`${data.result}`).result.name
this.from = JSON.parse(`${data.result}`).result.from
}
}
)
},2000)
}
build() {
Row() {
Column() {
Text(this.poem)
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text(this.from)
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
避免地獄回調(diào)文章來源:http://www.zghlxwxcb.cn/news/detail-761185.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-761185.html
import http from '@ohos.net.http'
@Entry
@Component
struct Index {
@State info:string = "hello Word !"
aboutToAppear(){
let httpReq = http.createHttp()
// httpReq.request返回的是promise,直接可以鏈?zhǔn)秸{(diào)用
let promise = httpReq.request('')
promise.then((data) =>{
//可以使用返回值作為參數(shù)繼續(xù)其它請(qǐng)求
this.info = JSON.parse(`${data.result}`).result.name
}).catch ((err) =>{
console.error(err)
})
}
build() {
Row() {
Column() {
}
.width('100%')
}
.height('100%')
}
}
到了這里,關(guān)于Arkts http數(shù)據(jù)請(qǐng)求的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!