android - 通过网络发送 bundle

标签 android android-networking

我正在尝试使用 ObjectInputStreams 等通过网络将 Bundle 从一台设备发送到另一台设备。问题是 Bundle不可序列化的。有没有一种简单的方法可以实现这一点?我想使用 Bundle 因为我可以发送各种类型(String、int、parcelables、列表等)

最佳答案

您不应该序列化 Bundle,因为它不能保证版本兼容性。因此,如果您可以确保每个设备都使用完全相同的版本,那么应该没问题。但我真的不会推荐这个。每个 api 都声明不应该这样做!

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

https://developer.android.com/reference/android/os/Parcel.html

但是如果你真的想这样做,这里有一个实现它的方法:

Bundle in = ...;
OutputStream os = ...

Parcel p = Parcel.obtain(); //create empty parcel object
in.writeToParcel(p, 0); //saving bundle as parcel
os.write(p.marshall()); //write parcel to stream

关于android - 通过网络发送 bundle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212628/

相关文章:

android - 实现 Retrofit2 服务接口(interface)

android - Firebase:如何授予添加新节点的权限,但只有所有者才能修改它们?

android - 如何将 CSS 应用于 Android 中的电子邮件正文?

c# - Android 通过 tcp 套接字从 c# 服务器接收图像

android - 如何检测android中的主机可达性?

android - 使用 OkHttp 时是否可以限制带宽?

android - 在不打开浏览器的情况下使用路标 OAuth 获取 AccessToken(双足 Oauth)

java - 不使用 int onebyte 将字符串写入 OutputStream

android - flutter 页面浏览量 : Find first and last page and swipe directions

java - Android 禁用其他应用程序的方法