我在理解如何在Java中使用Protocol Buffers方面遇到了问题。
我的client.proto
文件:
message MyMessage {
required int32 tag = 1;
extensions 100 to max;
}
extend MyMessage {
optional Hello hello = 100;
optional Login login = 101;
}
message Hello {
required uint32 version = 1;
}
message Login {
required string username = 1;
required string password = 2;
}
我想MyMessage
发送Hello
。我可以使用构建器创建MyMessage
或Hello
对象,但如何将Hello
添加到MyMessage
?我尝试过如下,但我得到一个例外:
Client.MyMessage.Builder mm = Client.MyMessage.newBuilder();
mm.setTag(100);
Client.Hello hello = Client.Hello.newBuilder().setVersion(1).build();
u.setExtension(Client.hello, h); // exception here
Client.MyMessage message = mm.build();
例外:
Caused by: java.lang.IllegalArgumentException: Failed to parse protocol buffer descriptor for generated code.
at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:311)
at com.test2.Client.<clinit>(Client.java:4153)
at com.test2.MainActivity$Net.send(MainActivity.java:383)