通过axios接收protobuf数据并使用classTransformer解析

通过axios接收protobuf数据并使用classTransformer解析

HarmonyOS
2024-06-11 19:54:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
davis_li

Protobuf是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化 。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前有大量应用都使用Protobuf来传输网络数据。

核心代码:

添加protobuf依赖:

proto消息体:

服务端首先创建Message实例,然后将其序列化为字节数组,为了避免接收数据有误,还需要将结果Base64编码后发送。

服务端代码:

客户端使用axios发送请求接收数据,先将结果Base64解码为Uint8Array,再由protobuf还原数据,最后使用class-transformer将数据解析成实体类。

此外,对于int64类型,在js\ts\ArkTs中没有对应的Long类型,会解析成number类型,只能正确显示16位,超出部分精度会丢失,可以引入long库来解决此问题。

页面代码:

import { user } from "./user"; 
import { ClassUser } from './ClassUser' 
import * as $protobuf from "@ohos/protobufjs"; 
import Long from 'long' 
import { plainToClass } from 'class-transformer'; 
import axios, { AxiosError, AxiosResponse } from '@ohos/axios' 
import { util } from '@kit.ArkTS'; 
 
$protobuf.util.Long = Long; 
$protobuf.configure() 
 
@Entry 
@Component 
struct Index { 
  @State result: string = ""; 
  str: string = ''; 
  scroller: Scroller = new Scroller() 
 
  doGet(url: string) { 
    axios.get(url) 
      .then((response: AxiosResponse) => { 
        // 将Base64解码为Uint8Array 
        let array: Uint8Array = new util.Base64Helper().decodeSync(response.data) 
        console.log("TAG== unit8Array: " + array) 
        let obj = user.UserLoginResponse.decode(array) 
        let res = plainToClass(ClassUser, obj); 
        console.log("TAG== res: " + JSON.stringify(res)); 
        this.result = "\nsessionId: " + res.sessionId + "\nuserPrivilege: " + res.userPrivilege + "\nisTokenType: " + res.isTokenType + 
          "\nformatTimestamp: " + res.formatTimestamp + "\naaa: " + res.aaa + "\nbbb: " + res.bbb 
      }) 
  } 
 
  build() { 
    Stack({ alignContent: Alignment.TopStart }) { 
      Scroll(this.scroller) { 
        Column() { 
          Text("发送请求") 
            .width('90%') 
            .height(60) 
            .backgroundColor(0xFFFFFF) 
            .borderRadius(15) 
            .fontSize(16) 
            .textAlign(TextAlign.Center) 
            .margin({ top: 20 }) 
            .onClick(() => { 
              this.doGet("http://10.182.68.200:8080/hello/getUint8array") 
            }); 
 
          Text("结果 :\r\n  " + this.result) 
            .fontSize(15) 
            .margin({ top: 55, left: 10, right: 10 }) 
 
        }.width('100%'); 
      } 
      .scrollable(ScrollDirection.Vertical).scrollBar(BarState.On) 
      .scrollBarColor(Color.Gray).scrollBarWidth(30); 
      Button('重置显示', { type: ButtonType.Capsule, stateEffect: true }) 
        .backgroundColor(0x317aff) 
        .margin({ top: 410, left: 5 }) 
        .width(110) 
        .onClick(event => { 
          this.result = ""; 
        }); 
    }.width('100%').height('100%').backgroundColor(0xDCDCDC) 
  } 
}
分享
微博
QQ
微信
回复
2024-06-12 16:19:41
相关问题
鸿蒙上如何使用js的protobuf
2418浏览 • 1回复 待解决
如何对JSON数据进行解析优化
241浏览 • 1回复 待解决
关于如何使用鸿蒙接收视频流
2043浏览 • 0回复 待解决
xml文件数据解析 ,都有哪些步骤?
260浏览 • 1回复 待解决
Mysql 驱动为什么要依赖 protobuf
2615浏览 • 1回复 待解决
解析和操作ASN.1数据的工具
662浏览 • 1回复 待解决
dlopen打开.so文件使用md5加密
357浏览 • 1回复 待解决
如何导出设备中的数据库文件查看
610浏览 • 1回复 待解决