diff --git a/entry/src/main/ets/common/utils/HttpUtil.ets b/entry/src/main/ets/common/utils/HttpUtil.ets index 0e251f59116e5251ea739e877317f619496644ff..c977a815c0285485db1af34d3bc40ae73a98283c 100644 --- a/entry/src/main/ets/common/utils/HttpUtil.ets +++ b/entry/src/main/ets/common/utils/HttpUtil.ets @@ -15,6 +15,8 @@ import { http } from '@kit.NetworkKit'; import CommonConstant from '../constant/CommonConstants'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; /** * Initiates an HTTP request to a given URL. @@ -28,11 +30,17 @@ export default async function httpGet(url: string) { } let request = http.createHttp(); - let result = await request.request(url, { - method: http.RequestMethod.GET, - header: { 'Content-Type': 'application/json' }, - readTimeout: CommonConstant.READ_TIMEOUT, - connectTimeout: CommonConstant.CONNECT_TIMEOUT - }); + let result: http.HttpResponse | undefined = undefined; + try { + result = await request.request(url, { + method: http.RequestMethod.GET, + header: { 'Content-Type': 'application/json' }, + readTimeout: CommonConstant.READ_TIMEOUT, + connectTimeout: CommonConstant.CONNECT_TIMEOUT + }); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'httpGet', `request failed, error code=${err.code}, message=${err.message}`); + } return result; } \ No newline at end of file diff --git a/entry/src/main/ets/pages/WebPage.ets b/entry/src/main/ets/pages/WebPage.ets index f3b9e0487861beeb974f0fbe737930db675fbe98..451e966371829d57db35abce45dc6e264fcc8dca 100644 --- a/entry/src/main/ets/pages/WebPage.ets +++ b/entry/src/main/ets/pages/WebPage.ets @@ -19,6 +19,8 @@ import { promptAction } from '@kit.ArkUI'; import httpGet from '../common/utils/HttpUtil'; import StyleConstant from '../common/constant/StyleConstant'; import CommonConstant from '../common/constant/CommonConstants'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component @@ -54,6 +56,7 @@ struct WebPage { left: $r('app.float.default_padding'), right: $r('app.float.default_padding') }) + Row() { Web({ src: this.webSrc, controller: this.controller }) .zoomAccess(true) @@ -64,6 +67,7 @@ struct WebPage { .height(StyleConstant.WEB_HEIGHT) .width(StyleConstant.FULL_WIDTH) .align(Alignment.Top) + Row() { Button(this.buttonName) .fontSize($r('app.float.button_font_size')) @@ -92,9 +96,12 @@ struct WebPage { this.controller.loadUrl(this.webSrc); } } catch (error) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.http_response_error') - }) + try { + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.http_response_error') }) + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'WebPage', `showToast failed, error code=${err.code}, message=${err.message}`); + } } } else { this.webVisibility = Visibility.Hidden;