尝试反序列化通过蓝牙发送的对象

时间:2019-03-13 16:50:11

标签: java android bluetooth deserialization

尝试反序列化通过蓝牙从一个Android设备发送到另一设备的对象。

对象大小为 126 个字节,我正好采用了该大小。但是我得到一个类未找到异常。

反序列化的代码为:

public static EcgDataPoint deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return (EcgDataPoint) o.readObject();
    }

错误:

tech.abhinavdas.bluetooth D/BluetoothReaderThread: Data type doesn't match
    java.lang.ClassNotFoundException: tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:628)
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1615)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1520)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1776)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
        at tech.abhinavdas.bluetooth.models.EcgDataPoint.deserialize(EcgDataPoint.java:54)
        at tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.deserializeAndRead(BluetoothReaderThread.java:47)
        at tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.run(BluetoothReaderThread.java:38)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/tech.abhinavdas.bluetooth-EZqoTdreOfJT1jU4xbF9dg==/base.apk"],nativeLibraryDirectories=[/data/app/tech.abhinavdas.bluetooth-EZqoTdreOfJT1jU4xbF9dg==/lib/arm64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:453) 
        at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:628) 
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1615) 
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1520) 
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1776) 
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) 
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) 
        at tech.abhinavdas.bluetooth.models.EcgDataPoint.deserialize(EcgDataPoint.java:54) 
        at tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.deserializeAndRead(BluetoothReaderThread.java:47) 
        at tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.run(BluetoothReaderThread.java:38) 

我想念什么?

1 个答案:

答案 0 :(得分:0)

您有两种具有相同名称的类型:

tech.abhinavdas.bluetooth.models.EcgDataPoint

tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint

所以:在这行代码中:

return (EcgDataPoint) o.readObject();

确保要转换的类型为tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint。

另外,请仔细检查您在发送方是否有不匹配的代码版本(这两个软件包的名称看起来极为相似)。