【问题标题】:Sending JSON to Bundle as serializable将 JSON 作为可序列化发送到 Bundle
【发布时间】:2020-03-18 05:09:30
【问题描述】:

我有一个扩展 JSONObject 并实现 MyInterface 的类,我必须将我的类的一个对象作为包发送到另一个可序列化的活动。

class MyClass extends JSONObject implements MyInterface{} 
interface MyInterface implements Serializable{}

我必须将 Myclass 的对象从一个活动发送到另一个具有实现 (MyInterface)getSerializable("data") 的活动,我面临的问题是我在第二个活动中收到的对象是空的。 在我的第一个活动中,我将对象设置为 -- bundle.putSerializableExtra("data",new MyClass(testData))

活动1

    `Intent i = new Intent(activity, TestActivity.class)try {
             Data param =new Data("{\n" +
                    "  \"test\": \"hello\",\n" +
                    "  \"datalist\": [\n" +
                    "    {\n" +
                    "      \"test\": \"hello\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"test\": \"hello\"\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}");
            i.putExtra("test1",param);
            i.putExtra("test2",param.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        activity.startActivity(i);
`

TestActivityCode--

`var data: MyInterface? = 
                 intent?.extras?.getSerializableExtra("test1") as MyInterface?`

数据类

       `class Data extends JSONObject implements MyInterface {
public Data(String data) throws JSONException {
        super(data);
    }
}`

【问题讨论】:

    标签: android serialization


    【解决方案1】:

    像这样传递你的对象:

    Intent intent = new Intent(activity, ChatListDetailActivity.class);
                        intent.putExtra("data", testData);
                        startActivity(intent);
    

    像这样获取getSerializable

    Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
               testData1 = (YorClassName) bundle.getSerializable("data");
            }
    

    【讨论】:

    • 请检查您的通过数据是否为空白@NehaRathore
    • 在放额外数据之前已经检查过了,也通过转换成字符串发送,在字符串中我得到了那个数据,但我必须通过可序列化来完成
    • 请用您当前的数据发送和获取代码更新您的问题。 @NehaRathore
    猜你喜欢
    • 2021-12-31
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2019-05-06
    • 2020-05-03
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多