gpt4 book ai didi

java - Protobuf 错误 :Protocol message tag had invalid wire type

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:17 29 4
gpt4 key购买 nike

我在尝试用 java 读取消息时遇到以下错误

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:78)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:438)

FileInputStream fis = new FileInputStream("F:/Newfolder/sample_message.txt");
Nt nlc = Nt.parseFrom(fis);

if(nlc.hasMessageId())
{
System.out.println("MessageId: "+nta2sse.getMessageId());
}

我在 if(nlc.hasMessageId()) 处遇到异常


这是完整的堆栈跟踪。

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:78)
at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:438)
at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse$Builder.mergeFrom(NtaSse.java:523)
at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse$Builder.mergeFrom(NtaSse.java:1)
at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:1)
at com.google.protobuf.AbstractMessageLite$Builder.mergeFrom(AbstractMessageLite.java:212)
at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:746)
at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:1)
at com.google.protobuf.AbstractMessageLite$Builder.mergeDelimitedFrom(AbstractMessageLite.java:282)
at com.google.protobuf.AbstractMessage$Builder.mergeDelimitedFrom(AbstractMessage.java:760)
at com.google.protobuf.AbstractMessageLite$Builder.mergeDelimitedFrom(AbstractMessageLite.java:288)
at com.google.protobuf.AbstractMessage$Builder.mergeDelimitedFrom(AbstractMessage.java:752)
at com.soeasy.aanta.nta.sse.NtaSse$Nta2Sse.parseDelimitedFrom(NtaSse.java:338)
at com.soeasy.aanta.nta.sse.NtaSseServer.main(NtaSseServer.java:60)

样本 _message.txt 具有以下内容:

message_id: 1
batch_meas_update {
device_update {
unique_device_id {
device_type: ME
device_id: 161
}
meas_update {
override_status: OVERRIDE_INACTIVE
bad_data_status: GOOD_DATA
scada_status: SCADA_ACTIVE
weight: 1.0
value: 406.596
}
}
}

是按照.proto文件

谢谢

最佳答案

我非常怀疑你在那里得到异常 - 我希望你在 parseFrom 中得到它。您能否发布完整 堆栈跟踪而不只是前三行?

我强烈怀疑您的文件基本上已损坏。您为应该是二进制 文件的内容提供了.txt 扩展名这一事实有点令人怀疑……该文件实际上是什么样子的?您不会像这样使用 parseFrom 来解析 protobuf 消息的 ASCII 表示。

编辑:根据评论中链接的问题,您正在尝试使用为二进制 数据设计的方法来解析文本 文件。

你想使用类似的东西:

// Use the normal try/finally for closing reliably
InputStreamReader reader = new InputStreamReader(fis, "ASCII");

Nt.Builder builder = Nt.newBuilder();
TextFormat.merge(reader, builder);
Nt nt = builder.build();

关于java - Protobuf 错误 :Protocol message tag had invalid wire type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6138721/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com