import http from '@ohos.net.http'
@Entry
@Component
struct Interface {
@State message: string = 'Hello World'
@State from: string = ''
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
aboutToAppear(){
//1.创建http对象 (在这之前需要再moudule.json5文件配置网络权限)
let httpRequest = http.createHttp()
// 2.发起请求
httpRequest.request("https://api.apiopen.top/api/sentences",{
method:http.RequestMethod.GET,
},(err,data)=>{
if (!err) {
this.message = JSON.parse(`${data.result}`).result.name
this.from = JSON.parse(`${data.result}`).result.from
}
})
}
}
自己拉的在线的接口,试一下吧!