【问题标题】:Trying to deserialize object sent over bluetooth试图反序列化通过蓝牙发送的对象
【发布时间】:2019-03-13 16:50:11
【问题描述】:

试图反序列化通过蓝牙从一台安卓设备发送到另一台的对象。

对象大小是 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) 

我错过了什么?

【问题讨论】:

  • “我错过了什么?” - 类路径中的正确类。找不到。
  • 您能详细说明或解释修复方法吗?谢谢!
  • 您的 EcgDataPoint 类位于何处?找不到
  • 它与试图反序列化它的类在同一个包中。

标签: java android bluetooth deserialization


【解决方案1】:

你有两种同名的类型:

tech.abhinavdas.bluetooth.models.EcgDataPoint

tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint

所以:在这行代码中:

return (EcgDataPoint) o.readObject();

确保您要投射的类型是 tech.abhinavdas.bluetoothpairingclient.models.EcgDataPoint。

另外,请仔细检查发送端的代码版本是否不匹配(这两个包的名称看起来非常相似)。

【讨论】:

  • 好像有问题..我会尝试强制包名相同。
  • 获取 java.io.StreamCorruptedException:无效流标头:java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:808) 处的 67446174 在 java.io.ObjectInputStream.(ObjectInputStream.java: 301) 在 tech.abhinavdas.datapoint.EcgDataPoint.deserialize(EcgDataPoint.java:67) 在 tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.deserializeAndRead(BluetoothReaderThread.java:50) 在 tech.abhinavdas.bluetooth.threads.BluetoothReaderThread.run (BluetoothReaderThread.java:38) @not-a-jd
  • 您在发送端使用 ObjectOutputStream 吗?
  • 是的,我是.. ByteArrayOutputStream b = new ByteArrayOutputStream();对象输出流 o = 新对象输出流(b); o.writeObject(this);返回 b.toByteArray();
猜你喜欢
  • 2013-03-21
  • 2015-11-24
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多