使用Retrofit 2 ProtoBuf转换器时出错

时间:2016-10-13 17:55:32

标签: java android protocol-buffers retrofit2

我正在尝试使用ProtoConverterFactory。我正在使用的.proto文件如下所示:

Foo.proto

syntax = "proto2";

import "bar.proto";

message CommonProtoMessage {
    required string messageName = 1;
    required bytes messageBody = 2;
}

message ResponseRequestMessage {

    optional Request1 request1 = 1;
    optional Response1 response1 = 2;
    optional Request2 request2 = 3;
    optional DoSomething dosomething = 4;
    optional GetResponseRequest getResponseRequest = 5;
    optional RequestCompletedNotification requestCompleted = 6;
}

message MessageWrapper {

    optional int64 requestId = 1;

    optional ResponseRequestMessage responseRequestMessage = 2;
    optional CommonProtoMessage commonMessage = 3;
}

Bar.proto

syntax = "proto2";

message Request1 {
    required string username = 1;
    required string password = 2;
    required string workstation = 3;
}

message Response1 {
    required int64 id = 1;
}

message Request2 {
    required int64 id = 1;
}

message DoSomething {
    required int64 id = 1;
}

message GetResponseRequest {
    required int64 id1 = 1;
    required int64 id2 = 2;
}

message RequestCompletedNotification {
    required int64 id1 = 1;
    required int64 id2 = 2;
}

我使用有线库将这些文件编译成几个java类。之后,我尝试使用Retrofit向服务器发送请求。

改造生成器:

Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)
            .addConverterFactory(ProtoConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();

但我的应用程序因错误而崩溃:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{**activity name**}: java.lang.IllegalArgumentException:
Unable     to     create @Body converter for class .proto.Foo 
(parameter #1)
Caused by: java.lang.IllegalArgumentException: Could not locate 
RequestBody converter for class 
proto.ngs.vms.messages.proto.MessageWrapper.

Tried:

* retrofit2.BuiltInConverters

* retrofit2.converter.protobuf.ProtoConverterFactory

从这个错误中我得到了Retrofit无法为我编译的类使用proto转换器

我试图在Retrofit和Protobuf上找到一些信息,但我找不到任何有用的信息

0 个答案:

没有答案