我是用python作服务器,发送数据到Android客户端。用的是同一个.proto文件分别用--python跟--java指令生成协议,分别添加到python跟Android。
但是Android端一直解析错误,错误提示如下: Caused by: com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
google了半天没找到正确的解决方法,请大神帮忙看看。以下是代码:
python代码:
entity = SubscribeRespProto_pb2.SubscribeRespProto()
entity.subReqID = 1
entity.userName = "H2901"
entity.productName = "qao"
entity.address = "guangzhou"
content = entity.SerializeToString()
print(str(entity))
proto.transport.write(content)
Android端用的是Netty4.1.5,关键代码如下:
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder())
.addLast(new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()))
.addLast(new ProtobufVarint32LengthFieldPrepender())
.addLast(new ProtobufEncoder())
.addLast(new SubReqClientHandler());
}
});
channelFuture = bootstrap.connect(Constants.IP, Constants.PORT).sync();
.proto文件如下:
message SubscribeRespProto{
required int32 subReqID = 1;
required string userName = 2;
required string productName = 3;
required string address = 4;
}