鸿蒙开发-网络请求

鸿蒙 网络请求

module.json5中添加一下内容

"requestPermissions": [  
  {  
    "name": "ohos.permission.INTERNET",  
    "reason": "$string:EntryAbility_desc",  
    "usedScene": {  
      "abilities": ["EntryAbility"],  
      "when": "always"  
    }  
  }  
],
import http from '@ohos.net.http'

const req = http.createHttp()
req.request('https://zhoushugang.gitee.io/fe2022/takeaway.json')
  .then(res => {
    console.log('MEITUAN', res.result.toString().replace(/\n/g, '').substr(0, 250))  // === 3  注意:字符长度大于1024字节,控制台不可见。
    const data = JSON.parse(res.result as string)
    console.log('MEITUAN', data.length)
  })
  .catch(err => {
    console.error('MEITUAN', err.message)
  })