From 0a9b2f02e8ec6367e7c9d52414d298f32ae9cb88 Mon Sep 17 00:00:00 2001 From: xuqm Date: Wed, 2 Apr 2025 19:01:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(basic):=20=E4=BC=98=E5=8C=96=20HTTP=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86=E5=92=8C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除了不必要的注释和未使用的代码 - 添加了数据发送进度的监听和日志记录- 优化了 HTTP 请求的 error 处理 - 调整了请求完成后的清理逻辑,移到 finally 块中执行- 引入了 ToolsHelper用于日志记录 --- src/main/ets/http/HttpHelper.ets | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/ets/http/HttpHelper.ets b/src/main/ets/http/HttpHelper.ets index bd58ec5..623e310 100644 --- a/src/main/ets/http/HttpHelper.ets +++ b/src/main/ets/http/HttpHelper.ets @@ -1,6 +1,6 @@ import { ArrayList, HashMap } from '@kit.ArkTS'; import http from '@ohos.net.http'; -import { LogHelper } from '../../../../Index'; +import { LogHelper, ToolsHelper } from '../../../../Index'; import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper'; import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys'; import { HttpHelperX, HttpParamsForm, HttpParamsGet, HttpParamsPost, HttpParamsUpload } from './HttpHelperX'; @@ -340,31 +340,24 @@ export class HttpHelper { if (showLog) { LogHelper.debug(`postJson:${apiNo}\n`, JSON.stringify(params)) } - // let data = HttpHelperX.getContent(params.data) - - - // const formData: http.MultiFormData[] = params.data as http.MultiFormData[] + httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => { + ToolsHelper.log(`${apiNo}:\n${JSON.stringify(data)}`) + }); httpRequest.request(HttpHelperX.getUrl(params.url, params.query), { method: http.RequestMethod.POST, connectTimeout: 20000, readTimeout: 20000, header: header, - extraData: params.data, usingCache: false, multiFormDataList: params.data, }) .then((data: http.HttpResponse) => { + if (showLog) { LogHelper.debug(`${apiNo}:\n ${data.result as string}`) LogHelper.print(data) } - if (this.httpHandlerList.hasKey(apiNo ?? params.url)) { - this.httpHandlerList.remove(apiNo ?? params.url) - SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList) - SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, - this.httpHandlerList.length) - } if (data.responseCode === 200) { resolve((typeof data.result === 'string' ? JSON.parse(data.result) : data.result) as T) } else { @@ -374,13 +367,8 @@ export class HttpHelper { reject(err) } }).catch((err: Error) => { + LogHelper.error(JSON.stringify({ err: err, url: params.url, })) - if (this.httpHandlerList.hasKey(apiNo ?? params.url)) { - this.httpHandlerList.remove(apiNo ?? params.url) - SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList) - SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, - this.httpHandlerList.length) - } if (err.message === 'Failed writing received data to disk/application') { const error: Error = new Error() error.name = 'cancel' @@ -389,6 +377,14 @@ export class HttpHelper { } else { reject(err) } + }).finally(() => { + httpRequest.off("dataSendProgress"); + if (this.httpHandlerList.hasKey(apiNo ?? params.url)) { + this.httpHandlerList.remove(apiNo ?? params.url) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerList, this.httpHandlerList) + SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.HttpHandlerListLength, + this.httpHandlerList.length) + } }); });