以下代码不起作用。这是使用Any? I'm trying to read from/write to
任何and identify the type stored by
任何人的最佳方式。
我的.proto
:
syntax = "proto3";
import "google/protobuf/any.proto";
message CommandListPrinters {
}
message Commands {
int32 id = 1;
repeated google.protobuf.Any command = 2;
}
my.java
:
CommandListPrinters commandListPrinters = CommandListPrinters.newBuilder().build();
Any any = Any.newBuilder()
.setValue(commandListPrinters.toByteString()).build();
Commands.Builder commandsBuilder = Commands.newBuilder().setId(0);
commandsBuilder.addCommand(any);
Commands commands = commandsBuilder.build();
//
byte [] ba = commands.toByteArray();
Commands cmds2 = Commands.parseFrom(ba);
for (Any any2 : cmds2.getCommandList()) {
Descriptor fe = any2.getDescriptorForType();
// This IF is FALSE;
if (fe.equals(CommandListPrinters.getDescriptor()) ) {
CommandListPrinters cmdLR = CommandListPrinters.parseFrom(any2.getValue());
}
}
答案 0 :(得分:0)
我是stackoverflow的新手,所以我无法评论,但我想知道: 为什么你的CommandListPrinters消息没有字段?
调用
CommandListPrinters.getDescriptor()
让我觉得CommandListPrinters应该有一个字段描述符。此外,Any proto也没有名为descriptor_for_type的字段。见https://github.com/google/protobuf/blob/master/src/google/protobuf/any.proto 所以
Descriptor fe = any2.getDescriptorForType();
也没有多大意义。