Protocol Buffers Java设置可选消息

时间:2016-07-27 09:38:29

标签: java protocol-buffers

我在理解如何在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。我可以使用构建器创建MyMessageHello对象,但如何将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)

0 个答案:

没有答案