HarmonyOS 鸿蒙Next API12关于axios的网络封装
HarmonyOS 鸿蒙Next API12关于axios的网络封装
<markdown _ngcontent-yfg-c237="" class="markdownPreContainer">
1.开发工具
DeEco Studio NEXT Developer Bate1
2.API版本
12
3.开发过程
安装axios
OpenHarmony三方库中心仓:https://ohpm.openharmony.cn/#/cn/home 项目中执行
ohpm install [@ohos](/user/ohos)/axios
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
主体业务实现(因实际业务api的返回值不是固定的所以未封装code判断到Util里面,可根据个人情况进行改造)HttpUtil.ets
import axios, { AxiosResponse } from '[@ohos](/user/ohos)/axios';
import ResponseResult from '../common/bean/ResponseResult';
import { http } from '@kit.NetworkKit';
import { Apis } from './Apis';
import Logger from './Logger';
const TAG = ‘[HttpUtil]’;
export class HttpUtil {
public static request(url: string, params: ESObject = {}, method = http.RequestMethod.GET,
contentType?: string): Promise<ResponseResult> {
axios.defaults.headers[‘Content-Type’] =
contentType || method === http.RequestMethod.POST ? ‘application/json’ : ‘application/x-www-form-urlencoded’;
let responseResult:Promise<AxiosResponse<string>> = axios.request({
url,
method,
params
});
let serverData: ResponseResult = new ResponseResult();
return responseResult.then((response: AxiosResponse<string>)=>{
Logger.info(TAG, “网络请求:” + “接口:” + Apis.BASE_Url + url + " 参数: " + JSON.stringify(params) + “返回结果:” + JSON.stringify(response.data));
if (response.status == http.ResponseCode.OK) {
serverData = JSON.parse(JSON.stringify(response.data));
} else {
serverData.resCode = Apis.BASE_ERROR_CODE;
serverData.resMsg = ${<span class="hljs-string"><span class="hljs-string">'网络错误,请联系管理员:'</span></span>}&${response.status}
;
}
return serverData;
}).catch((e:object) => {
Logger.info(TAG,“网络请求异常:”+ JSON.stringify(e))
serverData.resCode = Apis.BASE_ERROR_CODE;
serverData.resMsg = ‘网络错误,请联系管理员’;
return serverData;
})
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
业务调用HomeViewModel.ets(获取banner)
import BannerBean from '../common/bean/BannerBean';
import ResponseResult from '../common/bean/ResponseResult';
import { Apis } from '../utils/Apis';
import { HttpUtil } from '../utils/HttpUtil';
export class HomeViewModel {
getBannerList(): Promise<BannerBean[]> {
return new Promise((resolve: Function, reject: Function) => {
HttpUtil.request(Apis.BASE_Url+Apis.bannerList).then((res: ResponseResult) => {
if (res.resCode == 1) {
resolve(res.data);
} else {
reject(res.resMsg);
}
}).catch((res:object) => {
reject(JSON.stringify(res));
});
});
}
}
export default new HomeViewModel();
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
ResponseResult.ets
export default class ResponseResult {
resCode: number |string;
resMsg: string | Resource;
data: string | Object | ArrayBuffer | null;
constructor() {
this.resCode = 0;
this.resMsg = ‘’;
this.data = null;
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
Logger.ets
import { hilog } from '@kit.PerformanceAnalysisKit';
export class Logger {
private domain: number;
private prefix: string;
private format: string = %{public}s, %{public}s
;
constructor(prefix: string) {
this.prefix = prefix;
this.domain = 0xFF00;
}
debug(…args: string[]) {
hilog.debug(this.domain, this.prefix, this.format, args);
}
info(…args: string[]) {
hilog.info(this.domain, this.prefix, this.format, args);
}
warn(…args: string[]) {
hilog.warn(this.domain, this.prefix, this.format, args);
}
error(…args: string[]) {
hilog.error(this.domain, this.prefix, this.format, args);
}
fatal(…args: string[]) {
hilog.fatal(this.domain, this.prefix, this.format, args);
}
isLoggable(level: number) {
hilog.isLoggable(this.domain, this.prefix, level);
}
}
export default new Logger(’[WoNiu]’);
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
</markdown>关于HarmonyOS 鸿蒙Next API12关于axios的网络封装的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
找HarmonyOS工作还需要会Flutter技术的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17
我想问下 JSON.stringify可以打印出来res的json数据,但是res中的其他类 就无法打印出来 比如res.data都是没有反应的,是实体类的问题吗?
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
编译出Android APP,网络请求不能使用,什么问题?
First attempt: load module failed. dlopen failed: library "libshared_libz.so" not found
Second attempt: load module failed. dlopen failed: library "/data/user/0/com.example.myharmonytest/files/arkui-x/libs/arm64-v8a/libnet_http.so" not found
哪里没有配置对吗?
Android??
你用的啥arkui-X吗?
是的 arkui-x 试试跨平台的的可能性