将数据从Python发送到Java时出现InvalidProtocolBufferException

时间:2015-10-21 12:36:30

标签: java python redis protocol-buffers

我想在python中编码一个protopuf并通过redis发送到我java的{​​{1}}应用程序。 Atm我可以在decode app中打印数据并且值是正确的。但每次我收到数据时都会遇到以下异常:

java

我也和Jedis一起尝试过,但那里的数据不对。还试图发送它没有从python的bytearray强制转换,但我在这里得到相同的错误。

有没有人对这个问题有所了解?

InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag 方面的代码:

python

tele_bytes = array("B") // tele_bytes data comes from serial interface tele_bytes[1] = ser.read() tele_bytes[2] = ser.read() raw_data = ''.join(chr(x) for x in [ tele_bytes[1], tele_bytes[2] ]) gw_id = '12345678' reading = Reading_raw_data_pb2.ReadingRaw() reading.timestamp = int(time()) reading.gw_id = gw_id reading.raw_data = raw_data reading_string = reading.SerializeToString() r_server.lpush("testID", bytearray(reading_string)) 方面的代码:

Java

Protobuf文件python:

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);

            }

        }
    }
}

java的Protobuf文件:

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
}

0 个答案:

没有答案