import { Color, createRef, Gravity, gravity, Group, HLayout, IRequest, jsx, layoutConfig, log, modal, network, Panel, Scroller, Stack, Text, VLayout } from "doric"; import * as PubTool from "../PubTool" @Entry export class NetworkDemo extends Panel { build(rootView: Group): void { const padding = 15 const resGetText = createRef(); const resPostText = createRef(); const resRequestText = createRef(); 网络请求 GET请求 response: { modal(this.context).toast('请求中...', Gravity.Center) network(this.context).get('https://www.dmoe.cc/random.php?return=json').then(res => { log(`Response : ${res.data}`) const jsonStr = JSON.stringify(res) resGetText.current.text = jsonStr modal(this.context).alert(jsonStr) }).catch(e => { modal(this.context).toast('Catched:' + JSON.stringify(e)) }) }} > 发起请求 POST请求 response: { modal(this.context).toast('请求中...', Gravity.Center) const data = 'start=1&num=1' network(this.context).post('http://baobab.kaiyanapp.com/api/v4/discovery/hot', data).then(res => { log(`Response : ${res.data}`) const jsonStr = JSON.stringify(res) resPostText.current.text = jsonStr modal(this.context).alert(jsonStr) }).catch(e => { modal(this.context).toast('Catched:' + JSON.stringify(e)) }) }} > 发起请求 通用request请求 response: { modal(this.context).toast('请求中...', Gravity.Center) var r: IRequest = { url: 'http://baobab.kaiyanapp.com/api/v4/discovery/hot', method: 'post', data: 'start=1&num=1' } network(this.context).request(r).then(res => { log(`Response : ${res.data}`) const jsonStr = JSON.stringify(res) resRequestText.current.text = jsonStr modal(this.context).alert(jsonStr) }).catch(e => { modal(this.context).toast('Catched:' + JSON.stringify(e)) }) }} > 发起请求 } }