HarmonyOS http请求回调后,showToast报错。

使用http.HttpRequest.request发起http请求后,在promise的then回调里执行自定义回调函数,但是回调到页面代码后,调用toast会报错[ArkRuntime Log] Error: Internal error. UI execution context not found.

操作步骤:

1、http.createHttp();创建请求

2、request.request;发起请求

3、promise.then中处理数据然后执行回调函数

4、回调函数中使用promptAction.showToast进行输出toast

HarmonyOS
2024-12-17 14:09:41
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

根据报错信息:Internal error. UI execution context not found.是在调用promptAction.showToast接口时,接口识别到场景下UI实例缺失主动抛出的。绑定UI实例来调用接口,同时进行合理的try catch异常捕捉。 demo如下:

@Entry
@Component
struct Index {
  @State message: string = '从窗口实例中获取UI实例,通过UI实例使用接口';

  aboutToAppear(): void {
    setTimeout(() => {
      //抓取异常
      try {
        this.getUIContext().getPromptAction().showToast({ message: '弹窗测试', duration: 2000 })
      } catch (e) {
        console.error("弹窗异常,异常信息:" + JSON.stringify(e))
      }
    }, 1000)
  }

  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(15)
        .fontWeight(FontWeight.Bold)
        .textAlign(TextAlign.Center)
        .width('100%')
    }.width('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-17 16:18:33
相关问题
网络请求如何进行
1466浏览 • 1回复 待解决
HarmonyOS http请求流数据报错
555浏览 • 1回复 待解决
HarmonyOS http请求下载文件报错2300023
495浏览 • 1回复 待解决
http请求报错2300006如何解决
3266浏览 • 1回复 待解决
支付成功没有收到
2182浏览 • 1回复 待解决
http请求中下载文件报错2300023
3011浏览 • 1回复 待解决
HarmonyOS http请求大于5M的图片报错
287浏览 • 1回复 待解决
HTTP请求传输大于5m文件报错2300023
885浏览 • 1回复 待解决
HarmonyOS 事件
505浏览 • 1回复 待解决