import http from '@ohos.net.http';
import promptAction from '@ohos.promptAction';
封裝HTTP接口請(qǐng)求類,提供格式化的響應(yīng)信息輸出功能。
使用 DevEco Studio 3.1.1 Release 及以上版本,API 版本為 api 9 及以上。
示例:
import { MyHttpUtil } from '../common/utils/MyHttpUtil';
async function fetchWeatherData() {
const request = await MyHttpUtil.request('https://api.oioweb.cn/api/weather/GetWeather', 'GET', {}, true);
}
鴻蒙OS開(kāi)發(fā) | 更多內(nèi)容↓點(diǎn)擊 | HarmonyOS與OpenHarmony技術(shù) |
---|---|---|
鴻蒙技術(shù)文檔 | 開(kāi)發(fā)知識(shí)更新庫(kù)gi tee.com/li-shizhen-skin/harmony-os/blob/master/README.md 在這。 | 或+mau123789學(xué)習(xí),是v喔 |
// 定義日志標(biāo)識(shí)符
private static readonly LOG_TAG: string = "====MyHttpUtil";
/**
* 發(fā)起HTTP請(qǐng)求的方法封裝.
* @param url API 地址,若不包含協(xié)議頭,則自動(dòng)添加當(dāng)前應(yīng)用的主機(jī)域名。
* @param method 請(qǐng)求方法,如 'GET', 'POST' 等。
* @param params 當(dāng)HTTP請(qǐng)求方法為GET、OPTIONS、DELETE、TRACE、CONNECT時(shí),此參數(shù)用于傳遞查詢字符串;對(duì)于POST方法,這些數(shù)據(jù)會(huì)被作為請(qǐng)求體內(nèi)容。
* @param showErrorToast 若為 true,在接口業(yè)務(wù)錯(cuò)誤時(shí),向用戶顯示 toast 提示信息。
* @returns 成功時(shí)返回接口響應(yīng)數(shù)據(jù),請(qǐng)求異常時(shí)返回 undefined。
*/
public static async request(url: string, method: string, extraData: object, showErrorToast: boolean): Promise< any | undefined > {
try {
if (!url) {
return undefined;
}
console.info(`${MyHttpUtil.LOG_TAG}: Request started with URL:`, url);
let request = http.createHttp();
let options = {
method: method, //http.RequestMethod.GET 或 http.RequestMethod.POST
header: {
'Content-Type': 'application/json'
},
readTimeout: 50000, //讀取超時(shí)時(shí)間。單位為毫秒(ms),默認(rèn)為60000ms。 設(shè)置為0表示不會(huì)出現(xiàn)超時(shí)情況。
connectTimeout: 50000, //連接超時(shí)時(shí)間。單位為毫秒(ms),默認(rèn)為60000ms。
extraData: extraData,
} as http.HttpRequestOptions;
let result = await request.request(url, options);
result = JSON.parse(JSON.stringify(result))
console.info(MyHttpUtil.LOG_TAG, 'request end url:', url); //請(qǐng)求結(jié)束后
console.info(MyHttpUtil.LOG_TAG, 'request method:', method);
console.info(MyHttpUtil.LOG_TAG, 'request extraData:', JSON.stringify(extraData));
// console.info(MyHttpUtil.LOG, 'request result', JSON.stringify(result, null, 2));
console.info(MyHttpUtil.LOG_TAG, 'request result', JSON.stringify(result));
if (result.responseCode == 200) {
console.info(MyHttpUtil.LOG_TAG, 'request code 200 result', result.result.toString());
console.info(MyHttpUtil.LOG_TAG, 'request code 200 result', JSON.stringify(JSON.parse(result.result.toString()), null, 2));
// console.info(MyHttpUtil.LOG, 'request code 200 result', JSON.parse(result.result.toString()));
}
//判斷業(yè)務(wù)異常時(shí),彈出對(duì)應(yīng)的toast
if (showErrorToast) { //TODO 還需要追加自己業(yè)務(wù)的判斷
promptAction.showToast({
message: '這里打印接口業(yè)務(wù)的message錯(cuò)誤信息,根據(jù)自己公司接口業(yè)務(wù)情況封裝。',
duration: 2000,
bottom: '375lpx'
})
}
return result;
} catch (error) {
console.error(MyHttpUtil.LOG_TAG, 'request end url:', url);
console.error(MyHttpUtil.LOG_TAG, 'request method:', method);
console.error(MyHttpUtil.LOG_TAG, 'request extraData:', JSON.stringify(extraData));
console.error(MyHttpUtil.LOG_TAG, 'request', JSON.stringify(error));
} finally {
return undefined
}
}
}
}
審核編輯 黃宇
-
HarmonyOS
+關(guān)注
關(guān)注
79文章
2005瀏覽量
31778 -
OpenHarmony
+關(guān)注
關(guān)注
26文章
3803瀏覽量
17782 -
鴻蒙OS
+關(guān)注
關(guān)注
0文章
190瀏覽量
4833
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
鴻蒙原生應(yīng)用開(kāi)發(fā)-網(wǎng)絡(luò)管理HTTP數(shù)據(jù)請(qǐng)求
基于STM32封裝的HTTP請(qǐng)求知識(shí)
鴻蒙應(yīng)用開(kāi)發(fā)請(qǐng)求不到數(shù)據(jù)是為什么?
鴻蒙 OS 應(yīng)用開(kāi)發(fā)初體驗(yàn)
http請(qǐng)求 get post
HTTP請(qǐng)求方式有哪些
【EsDA 應(yīng)用】快速實(shí)現(xiàn)串口轉(zhuǎn)HTTP請(qǐng)求

鴻蒙OS開(kāi)發(fā)實(shí)例:【工具類封裝-emitter組件間通信】
鴻蒙OS開(kāi)發(fā)實(shí)例:【工具類封裝-頁(yè)面路由】

鴻蒙OS開(kāi)發(fā)實(shí)例:【窺探網(wǎng)絡(luò)請(qǐng)求】

鴻蒙OS開(kāi)發(fā)實(shí)戰(zhàn):【網(wǎng)絡(luò)管理HTTP數(shù)據(jù)請(qǐng)求】

鴻蒙OS開(kāi)發(fā)實(shí)例:【HarmonyHttpClient】網(wǎng)絡(luò)框架

評(píng)論