Protobuf 错误:协议消息标签的线类型无效 [英] Protobuf error:Protocol message tag had invalid wire type

查看:427
本文介绍了Protobuf 错误:协议消息标签的线类型无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having the following error when trying to read the message in 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 具有以下内容:

and the sample _message.txt has the following:

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 中得到它.你能发布完整堆栈跟踪而不是前三行吗?

I very much doubt that you're getting the exception there - I'd expect you to get it in parseFrom. Could you post the full stack trace instead of just the first three lines?

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

I strongly suspect you've basically got a broken file. The fact that you've given a .txt extension for what should be a binary file is somewhat suspect... what does the file actually look like? You don't use parseFrom like this to parse an ASCII representation of a protobuf message.

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

As per the question linked in the comment, you're trying to parse a text file using a method designed for binary data.

您想使用以下内容:

// 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();

这篇关于Protobuf 错误:协议消息标签的线类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆