HarmonyOS 鸿蒙Next网络请求返回的结果,能实现自动判断类型吗?
HarmonyOS 鸿蒙Next网络请求返回的结果,能实现自动判断类型吗?
<markdown _ngcontent-urj-c237="" class="markdownPreContainer">
网络请求返回结果一复杂,定义接受类型就比较麻烦,有没有自动判断类型并且可以接受请求结果的实现方法
请求方法
import { AxiosHeaders } from '[@ohos](/user/ohos)/axios';
import { axiosClient } from '../http/AxiosClient'
const commonHeader: AxiosHeaders = new AxiosHeaders()
export function getArticleDetail(data: articleDetailReq) {
return axiosClient.post<articleDetailRes>({
url: ‘/article/draft/detail’,
data: data,
showLoading: true
})
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
// 接口响应类型
export interface articleDetailRes {
code: string
msg: string
data: articleDetailDataResponse
}
// 数据结构
export interface articleDetailDataResponse {
accApp: string;
accH5: string;
addTime: number;
addTimeDes: string;
articleId: number;
collectFlag: boolean;
collects: number;
commentFlag: boolean;
commentList: Array<CommonType>;
comments: number;
detailUrl: string;
editor: string;
goodsList: Array<CommonType>;
imageViews: Array<CommonType>;
images: number;
imgUrlCol: string;
isOriginal: number;
latitude: number;
location: string;
longitude: number;
outViews: number;
posterUrl: string;
praiseFlag: boolean;
praises: number;
pubTime: number;
pubTimeDes: string;
relateList: Array<CommonType>;
shareUrl: string;
shares: number;
source: string;
srcUrl: string;
summary: string;
tag: string;
title: string;
typeFlag: number;
videoUrl: string;
views: number;
virtualViews: number;
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
</markdown>
关于HarmonyOS 鸿蒙Next网络请求返回的结果,能实现自动判断类型吗?的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
get['result']['content']这种?
import {
efAxiosParams,
efClientApi,
} from '[@yunkss](/user/yunkss)/eftool'
@Entry
@Component
struct Page87 {
@State context:string = “”
build() {
Column({space:10}) {
Button(‘测试1’).onClick(() => {
this.requestTest()
})
Text(测试响应内容:${<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.context}
)
}
.height(‘100%’)
.width(‘100%’)
}
async requestTest(){
efAxiosParams.isConvertDTO = false //不需要响应数据转换为OutDTO则将efAxiosParams.isConvertDTO设置为false
const get = await efClientApi.get<string>(‘https://api.oioweb.cn/api/SoulWords’);
console.info(get:${<span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(get)}
)
console.info(content:${get[<span class="hljs-string"><span class="hljs-string">'result'</span></span>][<span class="hljs-string"><span class="hljs-string">'content'</span></span>]}
)
this.context = get[‘result’][‘content’]
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>