接口解析
2024-11-30 13:42:20
200次阅读
0个评论
最后修改时间:2024-11-30 13:45:12
接口解析post
POST接口连接:https://openatom.atomgit.com/api/developer/recommend_list
模型层 ApiResult
export interface ApiResult<T> {
code: number;
msg: string;
data: T;
}
Community
export interface Community {
barId:number;
barName:string;
barNameEn:string|null;
jumpUrl:string;
communityId:number;
sort:number;
flag:number;
createName:string;
createTime:string;
updateName:string|null;
updateTime:string
}
Developer
import { Recommend } from "./Recommend" //Developer包含Recommend
export interface Developer{
records:Recommend[];//数组
total:number;
size:number;
current:number;
pages:string;
}
Recommend
import { WarehouseList } from "./WarehouseList";
export interface Recommend{
nickname:string;
photo:string;
userId:string;
username:string;
profile:string;
jump:string;
isFocus:boolean;
focusButtonDisabled:boolean;
warehouseList:WarehouseList;
}
WarehouseList
export interface WarehouseList{
jump:string;
warehouseName:string
}
引包操作
import axios, { AxiosResponse } from '@ohos/axios';
import { BusinessError } from '@kit.BasicServicesKit';
import { Developer } from '../model/Developer';
import { Recommend } from '../model/Recommend';
返回数据
@State info: Developer | null = null;
更改最后部分
const result: AxiosResponse<WareHouse> =
await axios.get<ApiResult<WareHouse>, AxiosResponse<WareHouse>, null>(url, {
headers: {
"X-ATOMGIT-POP-COMMUNITY": "openatom"
}
});
遍历展示数据 url地址一定要更改
import axios, { AxiosResponse } from '@ohos/axios';
import { BusinessError } from '@kit.BasicServicesKit';
import { Developer } from '../model/Developer';
import { Recommend } from '../model/Recommend';
PersistentStorage.persistProp<string>("token", "");
@Entry
@Component
struct ShowIndex {
scroller: Scroller = new Scroller();
@State url: string = "https://openatom.atomgit.com/api/developer/recommend_list";
@State info: Developer | null = null;
build() {
Scroll(this.scroller) {
Column(){
ForEach(this.info?.records,(rec:Recommend,index)=>{
Column(){
Text(rec.nickname)
Image(rec.photo).width(50)
Text(rec.userId)
Text(rec.username)
Text(rec.jump)
}
})
}.width('100%')
}
}
aboutToAppear(): void {
this.getOrgList();
}
async getOrgList() {
interface RequestParam {
pageSize: number;
pageNum: number;
isSelected: number;
}
const param: RequestParam = {
pageSize: 10,
pageNum: 1,
isSelected: 0
};
axios.post(this.url, param, {
headers: {
"X-ATOMGIT-POP-COMMUNITY": "openatom"
}
}).then((ret: AxiosResponse) => {
this.info=ret.data["data"];
console.log(`请求结果:${JSON.stringify(ret.data["data"])}`);
}).catch((error: BusinessError) => {
console.error(`请求异常:${JSON.stringify(error)}`);
})
}
}
00
- 0回答
- 0粉丝
- 0关注
相关话题
- 接口解析
- 元服务——接口解析
- 元服务——5、接口解析
- 第五章接口解析示例
- 【HarmonyOS NEXT】ArkTs函数、类、接口、泛型、装饰器解析与使用
- 【芙莉莲教你写代码】元服务 DevEco Studio 第五章 接口解析
- OpenHarmony:Har工程依赖库接口二次导出
- HarmonyOS应用开发实战,半天实现知乎日报项目(二、网络接口的封装使用)
- HarmonyOS Next 架构深度解析
- HarmonyOS应用开发实战:半天实现知乎日报项目(九、知乎日报项目接口使用指南)
- HarmonyOS NEXT应用开发实战:一分钟写一个网络接口,JsonFormat插件推荐
- JSON.parse 解析错误分析
- 140.HarmonyOS NEXT系列教程之3D立方体旋转轮播案例讲解之DataChangeListener接口
- HarmonyNext深度解析与ArkTS实战指南
- 132.HarmonyOS NEXT系列教程之3D立方体旋转轮播案例讲解(三):接口类型系统