Is it possible to send serialized objects with android/kotlin and receive them with an plain java application?

kotlin · · 30 次点击    
<p>Is it possible to send serialized (&#34;public class Data implements Serializable&#34;) objects with android/kotlin and receive them with an plain java application, when I have the same class on both ends, but on android in kotlin code and the receiver part in java code?</p> <hr/>**评论:**<br/><br/>deinlandel: <pre><p>I would suggest not to use Java serialization for network communication. One day you might find yourself in situation when you want to receive data in Swift/Python/whatever application. So use something platform independent like JSON, protobuf, etc.</p></pre>SergeantFTC: <pre><p>There&#39;s other important reasons not to use Serializable for this as well. </p></pre>yawkat: <pre><p>Sure you can, but why exactly would you use java serialization? Java serialization really kind of sucks compared to the alternatives. And over udp nonetheless?</p></pre>wightwulf1944: <pre><p>This is exactly how android apps communicate with servers which may or may not be a java application.</p> <p>What you have to consider is what is the communication medium? Http? If it is, the most common way to de/serialize objects is to use the JSON format</p></pre>a2a2a2a2a2a2a2a2a2a2: <pre><p>No, I send my data via UDP. No other layers above it.</p></pre>Wolfsdale: <pre><p>You mentioned you need UDP so then your representation probably needs to fit within a single UDP packet. As such JSON may not work very well, but I would still advise against Java serialization. Instead, you can try Protobuf serialization, which should be much smaller than Java serialization as Java serialization includes the full class name (including package). If that&#39;s not an option, you could simply use your Java classes you wish to serialize/deserialize within your Kotlin project, as long as you keep the package the same. That is I think the most surefire way of getting Java serialization to work across platforms.</p></pre>0rpheu: <pre><p>If your Java DTOs are compatible, yes</p></pre>a2a2a2a2a2a2a2a2a2a2: <pre><p>How can I compare DTOs? </p></pre>0rpheu: <pre><p>What do you mean, compare? </p> <p>Just use Gson: <a href="https://github.com/google/gson" rel="nofollow">https://github.com/google/gson</a></p> <p>Kotlin data classes can help you too</p></pre>
30 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet