简体   繁体   English

如何通过tcp套接字将对象发送到Android设备?

[英]how can I send an object over tcp socket to an android device?

I have a program written in java which acts as a type of server. 我有一个用java编写的程序,它充当一种服务器。 I need to send an object over this in a Tcp socket to the phone. 我需要在Tcp套接字上将一个对象发送到手机。

Before now I have just been communicsting string data via PrintWriter and DataInputStream . 在此之前,我刚刚通过PrintWriterDataInputStream传递字符串数据。 I was going to try ObjectInputStream but I heard this might fail cross platform due to different java versions etc. My object is serilizable. 我打算尝试ObjectInputStream但我听说由于不同的java版本等,这可能会失败跨平台。我的对象是可邮件化的。

You could use a JSON serializer such as this one here to convert the object into a very readable string in whwich you could just use your normal PrintWriter system in place. 您可以使用此处的JSON序列化程序将对象转换为一个非常易读的字符串,只需使用普通的PrintWriter系统就可以了。 You would just write the string out to the stream and then restore it on the other end. 您只需将字符串写入流中,然后在另一端将其还原。 Simple. 简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM