【鸿蒙HarmonyOS NEXT】调用后台接口及List组件渲染

【鸿蒙HarmonyOS NEXT】调用后台接口及List组件渲染

一、环境说明

  1. DevEco Studio 版本:

  2. API版本:以12为主

二、调用后台接口及List组件渲染

  1. 后台接口及返回数据分析

    JSON数据格式如下:

    json 复制代码
    {
        "code": 0,
        "data": {
            "total": 6,
            "pageSize": 10,
            "totalPage": 1,
            "currPage": 1,
            "list": [
                {
                    "id": 31,
                    "biaoti": "标题1",
                    "neirong": "内容1",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人1",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian1.jpg",
                    "thumbsupnum": 1,
                    "crazilynum": 1,
                    "clicktime": "2024-08-05 12:00:04",
                    "clicknum": 4,
                    "addtime": "2024-08-05 11:59:29"
                },
                {
                    "id": 32,
                    "biaoti": "标题2",
                    "neirong": "内容2",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人2",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian2.jpg",
                    "thumbsupnum": 2,
                    "crazilynum": 2,
                    "clicktime": "2024-08-05 12:00:04",
                    "clicknum": 5,
                    "addtime": "2024-08-05 11:59:29"
                },
                {
                    "id": 33,
                    "biaoti": "标题3",
                    "neirong": "内容3",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人3",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian3.jpg",
                    "thumbsupnum": 3,
                    "crazilynum": 3,
                    "clicktime": "2024-08-05 12:00:04",
                    "clicknum": 3,
                    "addtime": "2024-08-05 11:59:29"
                },
                {
                    "id": 34,
                    "biaoti": "标题4",
                    "neirong": "内容4",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人4",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian4.jpg",
                    "thumbsupnum": 4,
                    "crazilynum": 4,
                    "clicktime": "2024-08-05 12:01:05",
                    "clicknum": 7,
                    "addtime": "2024-08-05 11:59:29"
                },
                {
                    "id": 35,
                    "biaoti": "标题5",
                    "neirong": "内容5",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人5",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian5.jpg",
                    "thumbsupnum": 5,
                    "crazilynum": 5,
                    "clicktime": "2024-08-05 12:00:04",
                    "clicknum": 5,
                    "addtime": "2024-08-05 11:59:29"
                },
                {
                    "id": 36,
                    "biaoti": "标题6",
                    "neirong": "<p>66232</p>",
                    "shipin": "",
                    "fabushijian": "2024-08-05 11:59:42",
                    "faburen": "发布人6",
                    "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian6.jpg",
                    "thumbsupnum": 6,
                    "crazilynum": 6,
                    "clicktime": "2024-08-05 12:00:50",
                    "clicknum": 12,
                    "addtime": "2024-08-05 11:59:29"
                }
            ]
        }
    }
  2. 新建后台数据相关的实体类

    新建common目录并在其下新建bean目录,然后在bean目录下新建NewsDataItem.ets类,用于封装整个后台JSON数据的最内层数据

    typescript 复制代码
    /**
     * 用于定义具体的数据项
     * {
     "id": 31,
     "biaoti": "标题1",
     "neirong": "内容1",
     "shipin": "",
     "fabushijian": "2024-08-05 11:59:42",
     "faburen": "发布人1",
     "fengmian": "http://localhost:8080/springboot49j18/upload/xinwen_fengmian1.jpg",
     "thumbsupnum": 1,
     "crazilynum": 1,
     "clicktime": "2024-08-05 12:00:04",
     "clicknum": 4,
     "addtime": "2024-08-05 11:59:29"
     }
     */
    
    export default class NewsDataItem {
      public id: number
      public biaoti: string
      public neirong: string
      public shipin: string
      public fabushijian: string
      public faburen: string
      public fengmian: string
      // API12的语法,如果构造函数中该属性作为形式参数且是可选参数时,要保持类型一致
      public thumbsupnum: number
      public crazilynum: number
      public clicktime: string
      public clicknum: number
      public addtime: string
    
      /**
       * 构造函数
       * @param id
       * @param biaoti
       * @param neirong
       * @param shipin
       * @param fabushijian
       * @param faburen
       * @param fengmian
       * @param thumbsupnum
       * @param crazilynum
       * @param clicktime
       * @param clicknum
       * @param addtime
       */
      constructor(id: number, biaoti: string, neirong: string, shipin: string, fabushijian: string,
        faburen: string, fengmian: string, thumbsupnum: number, crazilynum: number, clicktime: string,
        clicknum: number, addtime: string) {
        this.id = id
        this.biaoti = biaoti
        this.neirong = neirong
        this.shipin = shipin
        this.fabushijian = fabushijian
        this.faburen = faburen
        this.fengmian = fengmian
        this.thumbsupnum = thumbsupnum
        this.crazilynum = crazilynum
        this.clicktime = clicktime
        this.clicknum = clicknum
        this.addtime = addtime
      }
    }

    然后在bean目录下新建NewsData.ets类,用于封装整个后台JSON数据的中间层数据

    typescript 复制代码
    /**
     * 定义用于接收后台返回的json中间层数据
     * "data": {
         "total": 6,
         "pageSize": 10,
         "totalPage": 1,
         "currPage": 1,
         "list": []
     */
    import NewsDataItem from './NewsDataItem'
    
    export default class NewsData{
      public  total: number
      public  pageSize: number
      public  totalPage: number
      public  currPage: number
      public  list: Array<NewsDataItem>
    
      constructor(total: number, pageSize: number, totalPage: number, currPage: number, list: Array<NewsDataItem>) {
        this.total = total
        this.pageSize = pageSize
        this.totalPage = totalPage
        this.currPage = currPage
        this.list = list
      }
    }

    然后在bean目录下新建NewsResultData.ets类,用于封装整个后台JSON数据的最外层数据

    typescript 复制代码
    /**
     * 定义用于接收后台返回的json最外层数据
     * {
     *    code: 0,
     *    data: {}
     * }
     */
    import NewsData from './NewsData';
    
    export default class NewsResultData extends Object{
      public code: number;
      public data: NewsData;
    
      constructor(code: number,data: NewsData) {
        super();
        this.code = code;
        this.data = data;
      }
    }
  3. 新建ListComponentPage.ets页面,实现aboutToAppear()函数,然后在该函数中调用后台接口,最后用List组件渲染后台数据,具体代码如下:

    typescript 复制代码
    import NewsDataItem from '../common/bean/NewsDataItem'
    import { http } from '@kit.NetworkKit';
    import NewsResultData from '../common/bean/NewsResultData';
    import NewsData from '../common/bean/NewsData';
    
    @Preview
    @Entry
    @Component
    struct ListComponentPage {
      private httpReq = http.createHttp()
      @State listData: Array<NewsDataItem> = []
    
      aboutToAppear(): void {
        // 调用接口
        this.httpReq.request('http://localhost:8080/springboot/xinwen/list').then((resp) => {
          //console.info("---调用后台接口返回---", JSON.stringify(resp))
          if (resp !== undefined && resp !== null) {
            if (resp.responseCode == 200) {
              //console.info("---resp.result---", resp.result)
              let newsResult: NewsResultData = JSON.parse(resp.result.toString())
              //console.info("---newsData---", newsResult.code)
              if (newsResult !== undefined && resp !== null) {
                let newsData: NewsData = newsResult.data
                this.listData = newsData.list
              }
            }
          }
        }).catch((reason: object) => {
          console.info("---接口错误信息---", JSON.stringify(reason))
        })
      }
    
      build() {
        Column() {
          List({ space: 10 }) {
            ForEach(this.listData, (item: NewsDataItem) => {
              ListItem() {
                Text(`${item.biaoti}`)
                  .width('100%')
                  .height(100)
                  .fontSize(20)
                  .fontColor(Color.White)
                  .textAlign(TextAlign.Center)
                  .borderRadius(10)
                  .backgroundColor(0x007DFF)
              }
            })
          }
          // 分割线
          .divider({
            strokeWidth: 5,
            color: Color.Red,
            startMargin: 10,
            endMargin: 10
          })
          // 设置成水平方向
          // .listDirection(Axis.Horizontal)
          // 滑动事件
          .onScrollIndex((firstIndex: number, lastIndex: number) => {
            console.info('滑动起始位置索引值' + firstIndex)
            console.info('滑动结束位置索引值' + lastIndex)
          })
          .onDidScroll((scrollOffset: number, scrollState: ScrollState) => {
            console.info('滑动偏移量' + scrollOffset)
            console.info('当前滑动状态' + scrollState)
          })
          .onReachStart(() => {
            console.info('列表起始位置到达')
          })
          .onReachEnd(() => {
            console.info('列表末尾位置到达')
          })
          .onScrollStop(() => {
            console.info('列表滑动停止')
          })
        }
        .padding(12)
        .height('100%')
        .backgroundColor(0xF1F3F5)
    
      }
    }
  4. 重新编译,运行效果如下:

三、总结

有如下关键几点:

  1. 调用后台接口,需要引入http模块或者其他诸如Ajax、axios等网络请求模块;
  2. 如果是需要在页面被创建的时候调用后台接口,则需要在aboutToAppear()中进行调用,如需要在页面显示时需要调用后台接口加载数据,则可以在onPageShow()函数中进行调用。根据实际业务结合组件的生命周期进行合适的调用;
  3. 如使用JSON.parse报错Structural typing is not supported (arkts-no-structural-typing),其原因是引入import { JSON } from '@kit.ArkTS';模块中的JSON,需要将其删除掉,使用内置的JSON对象(不需要显示引用);
相关推荐
Random_index7 小时前
#Uniapp篇:支持纯血鸿蒙&发布&适配&UIUI
uni-app·harmonyos
鸿蒙自习室10 小时前
鸿蒙多线程开发——线程间数据通信对象02
ui·harmonyos·鸿蒙
SuperHeroWu713 小时前
【HarmonyOS】鸿蒙应用接入微博分享
华为·harmonyos·鸿蒙·微博·微博分享·微博sdk集成·sdk集成
zhangjr057515 小时前
【HarmonyOS Next】鸿蒙实用装饰器一览(一)
前端·harmonyos·arkts
诗歌难吟4641 天前
初识ArkUI
harmonyos
SameX1 天前
HarmonyOS Next 设备安全特性深度剖析学习
harmonyos
郭梧悠1 天前
HarmonyOS(57) UI性能优化
ui·性能优化·harmonyos
郝晨妤1 天前
鸿蒙原生应用开发元服务 元服务是什么?和App的关系?(保姆级步骤)
android·ios·华为od·华为·华为云·harmonyos·鸿蒙
Peace*1 天前
HarmonyOs鸿蒙开发实战(16)=>沉浸式效果第一种方案一窗口全屏布局方案
harmonyos·鸿蒙·鸿蒙系统
howard20052 天前
鸿蒙实战:页面跳转传参
harmonyos·跳转·router·传参