关于android aidl对象

时间:2012-12-26 08:51:55

标签: android aidl

我必须通过IPC传递我的对象,就像这个类

一样
public class ForIpc {
    JsonObject info = new JSONObject();
    public void add(String key, String value) {
       info.put(key,value);
    } 
}

ForIpc可以实现Parcelable,但是如何处理JsonObject?

2 个答案:

答案 0 :(得分:0)

您可以使用JSON字符串作为JSONObject的串行表示。

    @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(info.toString());
}

从包裹中读取时将其解析回来。

info = new JSONObject(parcel.readString());

答案 1 :(得分:0)

您找到了解决方案吗?

我还没有找到更好的方法,但是阅读文档我相信你不能这样做(可以改正!)。我最终在我的Parcelable实现中使用了字符串(和其他允许的基本类型)。并将它留给客户端在String和JSONObject之间进行转换。所以客户必须这样做:

new JSONObject(parcelable.getStringContent())