HarmonyOS鸿蒙Next中Web组件加载超时ERR_TIMED_OUT问题分析

HarmonyOS鸿蒙Next中Web组件加载超时ERR_TIMED_OUT问题分析 通过app Web组件加载的时候出现加载超时(错误:ERR_TIMED_OUT),实际网络畅通,在浏览器可以正常访问,包括报错的url。在app访问其他域名的接口(非Web组件方式访问)也可以正常请求。后面未切换网络,重新安装app之后,网络就恢复正常。(问题偶现,不能稳定复现,但多次出现)

1、连接wifi A之后并且开启charles代理, 2、然后切换到热点wifi B(未开启代理) 3、在App跳转Web页面不能正常加载,在Web组件中onErrorReceive收到错误提示如下:

errorCode: -7 errorInfo: ERR_TIMED_OUT isMainFrame: true isRedirect: false isRequestGesture: false


核心代码片段:

```javascript
Web({ src: this.targetUrl, controller: this.webController })
  .width('100%')
  .layoutWeight(1)
  .domStorageAccess(true)
  .mixedMode(MixedMode.All)
  .javaScriptProxy(this.webManager.bridgeProxy)
  .onShowFileSelector((event) => {
    const photoSelectOptions = new picker.PhotoSelectOptions()
    let uri: string | null = null
    const photoViewPicker = new picker.PhotoViewPicker()
    photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
      uri = photoSelectResult.photoUris[0]
      if (event) {
        event.result.handleFileList([uri])
      }
    }).catch((err: BusinessError) => {
      Log.error('WebView onShowFileSelector error:', err)
    })
    return true
  })
  .onLoadIntercept((event) => {
    //false 表示允许加载
    return this.webManager.interceptUrl(event.data.getRequestUrl()) || false;
  })
  .onRenderProcessNotResponding((event) => {
    Log.info(`WebView onRenderProcessNotResponding: reason: ${event.reason}, jsStack: ${event.jsStack}`)
  })
  .onErrorReceive((event) => {
    if (event.request.isMainFrame()) {
      ToastUtil.show('网络不给力,请稍后再试~')
    }
    this.webManager.updateLoadStatus(LoadStatus.Failed)
    this.printError(event)
    console.info(`onErrorReceive code: ${event.error.getErrorCode()}, msg: ${event.error.getErrorInfo()}`)
  })
  .onHttpErrorReceive(() => {
    Log.info('onHttpErrorReceive');
    this.webManager.updateLoadStatus(LoadStatus.Failed)
  })
  .onSslErrorEventReceive((event) => {
    event?.handler?.handleConfirm()
    Log.info(`WebView onSslErrorEventReceive: ${event?.error}`)
  })
  .onPageEnd(() => {
    this.webManager.updateLoadStatus(LoadStatus.Succeed)
  })
  .onTitleReceive((event) => {
    this.onTitleReceive?.(event.title)
  })
  .onPageBegin(() => {
    this.webManager.updateLoadStatus(LoadStatus.Loading)
    Log.info('WebView onPageBegin')
  })
  .onProgressChange((event) => {
    if (!this.isShowProcess) return
    this.currProcess = event.newProgress
    Log.log(`WebView currProcess: ${this.currProcess}`)
  })

更多关于HarmonyOS鸿蒙Next中Web组件加载超时ERR_TIMED_OUT问题分析的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

跟wifi配置代理可能有关系,此错误发生在网络请求没有得到响应,超过了操作时间的情况下,基本原因都是和网络有关系,加载不出来的时候就是会白屏

更多关于HarmonyOS鸿蒙Next中Web组件加载超时ERR_TIMED_OUT问题分析的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,Web组件加载超时ERR_TIMED_OUT问题通常是由于网络请求在指定时间内未能完成导致的。可能的原因包括网络连接不稳定、服务器响应时间过长、DNS解析问题或URL格式错误。

首先,检查网络连接是否正常,确保设备能够访问互联网。其次,确认服务器是否正常运行,且响应时间在合理范围内。如果服务器响应时间过长,可能需要优化服务器性能或增加超时时间。

此外,检查URL是否正确无误,确保没有拼写错误或非法字符。如果URL中包含特殊字符,确保其已正确编码。DNS解析问题也可能导致加载超时,尝试使用IP地址直接访问以排除DNS问题。

在开发过程中,可以通过设置Web组件的超时时间来调整加载超时的阈值。例如,使用setLoadTimeout方法设置更长的超时时间,以适应网络环境的变化。

如果问题依然存在,可以尝试使用其他网络库或工具进行网络请求,以确定是否为Web组件的特定问题。同时,查看系统日志和网络日志,获取更多详细的错误信息,帮助进一步定位问题。

最后,确保HarmonyOS系统和Web组件库已更新到最新版本,以获得最新的修复和优化。通过以上步骤,可以有效分析和解决HarmonyOS鸿蒙Next中Web组件加载超时ERR_TIMED_OUT问题。

在HarmonyOS鸿蒙Next中,Web组件加载超时ERR_TIMED_OUT问题通常与网络连接、服务器响应或配置有关。首先,检查网络是否稳定,确保设备能够正常访问目标URL。其次,确认服务器响应时间是否过长,优化服务器性能或增加超时时间。最后,检查Web组件的配置,如setJavaScriptEnabledsetCacheMode,确保其适合当前应用场景。通过这些步骤,可以有效解决加载超时问题。

回到顶部