• 鸿蒙实战开发:数据交互【RPC连接】


    概述

    本示例展示了同一设备中前后台的数据交互,用户前台选择相应的商品与数目,后台计算出结果,回传给前台展示。

    样例展示

    基础信息

    image.png

    RPC连接

    介绍

    本示例使用[@ohos.rpc]相关接口,实现了一个前台选择商品和数目,后台计算总价的功能,使用rpc进行前台和后台的通信。

    效果预览

    主页选择列表

    使用说明:

    1. 点击商品种类的空白方框,弹出商品选择列表,选择点击对应的商品,空白方框显示相应内容。
    2. 点击商品选择框后的 +- 按钮,选择商品所对应的数量。
    3. 点击 Confirm an order 按钮,根据相应的菜品数量与单价,计算出总价并显示。

    具体实现

    • 发送数据:在首页的sortString()中通过rpc.MessageSequence.create()创建MessageSequence对象,然后通过MessageSequence.writeStringArray()将 我们的处理过的购物数据写入MessageSequence对象中,通过rpc.RemoteObject.sendMessageRequest()将我们得出总价所需要的参数发送到进程中, 源码参考:[Index.ets]
    /*
    
     * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
    
     * Licensed under the Apache License, Version 2.0 (the "License");
    
     * you may not use this file except in compliance with the License.
    
     * You may obtain a copy of the License at
    
     *
    
     *     http://www.apache.org/licenses/LICENSE-2.0
    
     *
    
     * Unless required by applicable law or agreed to in writing, software
    
     * distributed under the License is distributed on an "AS IS" BASIS,
    
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
     * See the License for the specific language governing permissions and
    
     * limitations under the License.
    
     */
    
    
    
    import rpc from '@ohos.rpc'
    
    import ReceivedData from '../model/ReceivedData'
    
    import { ProcessData } from '../model/ProcessData'
    
    import { Logger } from '../common/Logger'
    
    import { MyDataSource } from '../model/OptionsData'
    
    import { OPTIONS } from '../muck/MyData'
    
    import { TitleBar } from '../common/TitleBar'
    
    import { FlexList } from '../common/FlexList'
    
    
    
    const REQUEST_CODE: number = 1
    
    type SelectType = {
    
      subcategory: string,
    
      count: number
    
    }
    
    
    
    @Entry
    
    @Component
    
    struct Index {
    
      @State send: string = ''
    
      @State result: number = 0
    
      private Process: ProcessData = new ProcessData()
    
      private selectStuffs: Array = new Array(4).fill({ subcategory: '', count: 0 })
    
    
    
      selectStuff(index: number, good: string, count: number) {
    
        Logger.info(`index=  ${index}, count= ${count}, good= ${good}`)
    
        this.selectStuffs[index] = { subcategory: good, count: count }
    
      }
    
    
    
      async sortString(sendData: Array) {
    
        Logger.info(`sortString is ${sendData}`)
    
        Logger.info(`sendData typeof ${typeof (sendData)}`)
    
        let option: rpc.MessageOption = new rpc.MessageOption()
    
        let data: rpc.MessageSequence = rpc.MessageSequence.create()
    
        let reply: rpc.MessageSequence = rpc.MessageSequence.create()
    
        Logger.info(`getCallingUid: ${ReceivedData.getCallingUid()}  getCallingPid: ${ReceivedData.getCallingPid()}`)
    
        data.writeStringArray(sendData)
    
        try {
    
          await ReceivedData.sendMessageRequest(REQUEST_CODE, data, reply, option)
    
        } catch (err) {
    
          Logger.error(`sortString failed, ${JSON.stringify(err)}`)
    
        }
    
        this.result = reply.readInt()
    
        reply.reclaim()
    
      }
    
    
    
      build() {
    
        Scroll() {
    
          Column() {
    
            TitleBar()
    
            LazyForEach(new MyDataSource(OPTIONS), (item, index) => {
    
              Row() {
    
                FlexList({
    
                  category: item.category,
    
                  menu: item.subcategory,
    
                  index: index,
    
                  selectStuff: this.selectStuff.bind(this)
    
                })
    
              }
    
            }, item => JSON.stringify(item))
    
    
    
            Column() {
    
              Row() {
    
                Text($r('app.string.final_price'))
    
                  .width('65%')
    
                  .height(25)
    
                  .fontSize(18)
    
                  .textAlign(TextAlign.Center)
    
    
    
                Text(this.result.toString())
    
                  .id('totalPrice')
    
                  .height(25)
    
                  .fontSize(20)
    
                  .margin({ left: '5%' })
    
                  .textAlign(TextAlign.Center)
    
              }
    
              .justifyContent(FlexAlign.Center)
    
              .width('80%')
    
    
    
              Button($r('app.string.confirm'))
    
                .id('confirmOrderBtn')
    
                .width('80%')
    
                .height(40)
    
                .margin({ top: 30 })
    
                .onClick(() => {
    
                  let priceArray = this.Process.getPrice(this.selectStuffs)
    
                  Logger.info(`priceArray= ${priceArray}`)
    
                  this.sortString(priceArray)
    
                })
    
            }
    
            .width('100%')
    
            .justifyContent(FlexAlign.Center)
    
            .margin({ top: 20 })
    
            .height('30%')
    
          }
    
          .width('100%')
    
        }
    
      }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 读取数据:处理MessageRequest请求的接口封装在ReceivedData里面,在这里接收传递来的数据,然后经过处理得出总价, 并通过rpc.MessageParcel.writeInt()写入MessageParcel对象,源码参考:[ReceivedData.ets]
    /*
    
     * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
    
     * Licensed under the Apache License, Version 2.0 (the "License");
    
     * you may not use this file except in compliance with the License.
    
     * You may obtain a copy of the License at
    
     *
    
     *     http://www.apache.org/licenses/LICENSE-2.0
    
     *
    
     * Unless required by applicable law or agreed to in writing, software
    
     * distributed under the License is distributed on an "AS IS" BASIS,
    
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
     * See the License for the specific language governing permissions and
    
     * limitations under the License.
    
     */
    
    
    
    import rpc from '@ohos.rpc'
    
    import { Logger } from '../common/Logger'
    
    import { PRICE_LIST } from '../muck/MyData'
    
    
    
    const TAG = '[eTSRPC.ReceivedData]'
    
    let resultList: Array = Array.from({ length: 4 }, () => 0)
    
    
    
    class ReceivedData extends rpc.RemoteObject {
    
      onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean {
    
        Logger.info(TAG, `called`)
    
        if (code === 1) {
    
          let result = 0
    
          let readIntArr
    
          try {
    
            readIntArr = data.readStringArray()
    
          } catch (err) {
    
            Logger.error(`readStringArray failed, code is ${err.code}, message is ${err.message}`)
    
          }
    
          Logger.info(TAG, `readIntArr= ${readIntArr}`)
    
          for (let i = 0; i < 4; i++) {
    
            if (readIntArr[i] === '0') {
    
              resultList[i] = 0
    
            } else {
    
              resultList[i] = PRICE_LIST.get(readIntArr[i]) * Number(readIntArr[i+4])
    
            }
    
            Logger.info(TAG, `this.resultList= ${resultList}`)
    
            result += resultList[i]
    
          }
    
          try {
    
            reply.writeInt(result)
    
          } catch (err) {
    
            Logger.error(TAG, `writeInt failed, code is ${err.code}, message is ${err.message}`)
    
          }
    
          Logger.info(TAG, `result= ${result}`)
    
        } else {
    
          Logger.info(TAG, `unknown request code`)
    
        }
    
        return true
    
      }
    
    }
    
    
    
    export default new ReceivedData('send')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    鸿蒙OpenHarmony知识待更新

    c4239e28a4e48de5e1bbf2ecb8e239cd.jpeg

  • 相关阅读:
    LCR 144. 翻转二叉树
    短视频时代,亚马逊产品视频的作用是什么?对于提升Listing转化率究竟有何好处?
    如何在 Java 中实现无向图
    六、Hive数据仓库应用之Hive事务(超详细步骤指导操作,WIN10,VMware Workstation 15.5 PRO,CentOS-6.7)
    安卓Android_手机安装burp的https_CA证书
    使用HTML制作静态网站作业——我的校园运动会(HTML+CSS)
    Linux 线程控制 —— 线程取消 pthread_cancel
    面向对象的基础知识
    AliLinux的使用Docker初始化服务(详细)
    8Manage PM:通过项目管理信息系统做好进度管控
  • 原文地址:https://blog.csdn.net/2301_76813281/article/details/136488901