协议消息标签的电线类型无效 [英] Protocol message tag had invalid wire type

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

问题描述

我的应用程序通过protobuf从服务器发送数据到客户端。
当我在客户端上反序列化发送的有效负载时,eclipse会引发以下情况:

my application sends data through protobuf from a server to a client. When I am deserializing the sent payload on the client side eclipse throws a expection of the follogwing type:

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.

期望发生在我调用 parseFrom()时。我知道在大多数情况下,错误在于语法错误的protobuf文件中。因此,我希望在此处发布protobuf定义就足够了:

The expection happens when I call "parseFrom()". I know that in most cases the error lies in a protobuf file with the wrong syntax. Therefore I hope that it is enough to post the protobuf definition here:

   package protobuf;

       option java_package = "com.carproject.abs.demo.protobuf";
       option java_outer_classname = "DesktopDevice_getCarsResponse";

    message CARS {

        required int64 carid = 1;
        required string carname = 2;

        message Carinformation {
            required string street = 1;
            required string postalcode = 2;
            required string city = 3;
            required string country = 4;
            required string cartimezoneid = 5;
        }

        message Right {
            optional string name = 1;
            optional int32 type = 2;
            optional int32 service = 3;
        }

        message PropertyType {
            optional string name = 1;
            optional string value = 2;
        }

        repeated Carinformation carinformation = 3; 
        repeated Right carrights = 4;
        repeated PropertyType carproperties = 5;
        repeated string inoid = 6;
    }

下面的代码显示了如何在服务器端写入数据:

here is the code which shows how the data is written on the server side:

// carObj returns the necessary Strings
CAR carObj = car.getCAR();

Builder newBuilder = DesktopDevice_getCarResponse.CAR.newBuilder();

newBuilder.setCarid( carObj.getCARID() );
newBuilder.setCarname( carObj.getCARNAME());

// hardcoded values here
newBuilder.getCarinformationBuilder(1).setStreet( carObj.getCARNFORMATION().getSTREET() );
newBuilder.getCarinformationBuilder(1).setPostalcode( carObj.getCARINFORMATION().getPOSTALCODE() );
newBuilder.getCarinformationBuilder(1).setCity( carObj.getCARINFORMATION().getCITY() );
newBuilder.getCarinformationBuilder(1).setCountry( fleetObj.getCARINFORMATION().getCOUNTRY() );
newBuilder.getCarinformationBuilder(1).setCartimezoneid( fleetObj.getCARINFORMATION().getCARTIMEZONEID() );

byte[] responsePayload = newBuilder.build().toByteArray();
RestServerResponseMessage responseMsg = new RestServerResponseMessage( requestMsg.getRequestId(), responsePayload, "XML");
return responseMsg;

服务器使用protobuf提供的Builder模式来设置必要的字符串。然后将数据序列化为byte [],并通过protobuf发送回客户端。

As you can see, the Server uses the Builder pattern provided by protobuf to set the necessary strings. Then the data is serialized as a byte[] and is sent back to the client via protobuf.

这是我尝试解析数据的客户端代码。

Here is the client code where I try to parse the data.

HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream instream = entity.getContent();
        CAR car = DesktopDevice_getCarsResponse.CARS.parseFrom(instream);
        }

调用.parseFrom时会引发异常。服务器代码和carObj正常工作。我已经在程序中成功发送了protobuf数据。

The exception is thrown when .parseFrom is called. Server code and carObj are working fine. I already sucessfully sending protobuf data in my program.

推荐答案

使用 Base64.getEncoder.encode(protoMsg。 toByteArray)在服务器上。并使用 Base64.getDecoder.decode(receivedArray [Bytes])。在线传输的数据应始终进行编码和解码

Use Base64.getEncoder.encode(protoMsg.toByteArray) on server. And use Base64.getDecoder.decode(receivedArray[Bytes]). Data sent on wire should always be encoded and decoded

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

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