从Python向J发送数据时发生InvalidProtocolBufferException

2024-06-16 09:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python中编码一个protopuf,并通过redis将其发送到java应用程序,在那里我decode它。 Atm我可以在java应用程序中打印数据,并且值是正确的。但每次我收到数据时都会出现以下异常:

InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag

我也试过绝地武士,但那里的数据是错误的。也尝试在没有python的bytearray强制转换的情况下发送它,但是我在这里得到了相同的错误。在

有人对这个问题有什么想法吗?在

python侧的代码:

^{pr2}$

Java侧的代码:

import com.google.protobuf.InvalidProtocolBufferException;
import redis.clients.jedis.BinaryJedis;
import protos.Reading4Java;

import java.io.IOException;
import java.util.List;

public class SimpleSubByte {

    public static Reading4Java.ReadingRaw trRaw = null;

    public static void main(String[] args) {

        BinaryJedis binaryJedis = new BinaryJedis("test.id.local");

        while (true) {
            List<byte[]> byteArray = binaryJedis.brpop(0, "testID".getBytes());

            for (byte[] e :  byteArray) {
                // System.out.println(e);

                try {
                    trRaw = Reading4Java.ReadingRaw.parseFrom(e);
                } catch (InvalidProtocolBufferException e1) {
                    e1.printStackTrace();
                }

                System.out.println(trRaw);

            }

        }
    }
}

Protobuf文件python:

package ttm

message ReadingRaw {
    required string gw_id = 1;      // gateway id (e. g. mac address)
    required bytes raw_data = 2;    // raw data from serial interface
    optional int64 timestamp = 3;   // timestamp of data reading from sensor device
}

java协议文件:

package protos;

option java_outer_classname = "TireReading4Java";

message TireReadingRaw {
    required string gw_id = 1;      // gateway id (e. g. mac address)
    required bytes raw_data = 2;
    optional int64 timestamp = 3;
}

Tags: 数据importidmessagedatarawrequiredjava