简体   繁体   English

Android 将 JsonArray 从一个意图传递到另一个意图

[英]Android passing JsonArray from one intent to another

Been trying this for a while now.已经尝试了一段时间了。 I am trying to pass JsonArray from one intent to another.我正在尝试将 JsonArray 从一个意图传递到另一个意图。 Here is my code.这是我的代码。

Intent intent = new Intent(this, DownloadService.class);
Bundle bundle = new Bundle();
JSonArrayParser jSonArrayParser = new JSonArrayParser(dealTypeList);
bundle.putParcelable("jsonArray", jSonArrayParser);

intent.putExtra("deals_list", bundle);
this.startService(intent);

JSonArrayParser implements Parcelable. JSonArrayParser 实现 Parcelable。

I have checked that the JSonArrayParser is being populated with the Jsonarray as expected before passing it to the Bundle.在将 JSonArrayParser 传递给 Bundle 之前,我已经检查过是否按预期使用 Jsonarray 填充了 JSonArrayParser。

The issue is when I read it out on the other intent.问题是当我以另一个意图读出它时。 I get a null back for the JsonArray.我为 JsonArray 返回了 null。

Here is what I have so far.这是我到目前为止所拥有的。

if(intent.hasExtra("jsonArray"))
        {
            JSonArrayParser jSonArrayParser = null;
            Bundle bundle = intent.getParcelableExtra("jsonArray");
            if (bundle != null) {
                jSonArrayParser = bundle.getParcelable("deals_list");
                String jsonMyObject = bundle.getString("deals_list");

                jSonArrayParser = intent.getParcelableExtra("deals_list");
                if(jSonArrayParser != null)
                {
                    JSONArray jsonArray = jSonArrayParser.getJsonArray();
                    String ssad = "";
                }
            }
        }

I have tried a lot of different variation to try and read the value out but it seems the JSONArray is always null.我尝试了很多不同的变体来尝试读取值,但似乎 JSONArray 始终是 null。

Have searched through the inte.net before posting this.在发帖之前已经在 inte.net 上搜索过了。 Please if someone can help that would be great.如果有人可以提供帮助,那就太好了。

Thanks in advance.提前致谢。

EDIT:编辑:

I have tried passing JSonArray as string as following but that didnt seem to start the intent at all.我试过将 JSonArray 作为字符串传递如下,但这似乎根本没有启动意图。

Intent intent = new Intent(your_activity.this, new_activity.class);
intent.putExtra("jsonArray", dealTypeList.toString());
startActivity(intent);

I do your work like this . 我这样工作。 please you check this way. 请您以这种方式检查。

    Bundle extras = getIntent().getExtras();
    userName = extras.getString("user_name");

    Intent logIntent = new Intent(HomePage.this, LogIn.class);
    startActivity(logIntent);

retrieve data in other class. 检索其他类中的数据。

    private Bundle extraslogin = getIntent().getExtras();
    userName = extraslogin.getString("user_name");

and also you check , your data assigning part also.i think , this may be some help to you. 并且您还检查了数据分配部分。我认为,这可能对您有所帮助。 you try retrieve data other class , within 'onCreat' method. 您尝试在“ onCreat”方法内检索其他类的数据。 directly like above code. 直接像上面的代码。

I think you have used wrong keys... Please check.. 我认为您使用了错误的键...请检查..

You are putting bundle in Intent with key "deal_list". 您正在使用键“ deal_list”将捆绑包放入Intent。 and in bundle you have jsonarray with key "jsonArray". 并且在捆绑中,您有带有键“ jsonArray”的jsonarray。

So first u should check for key "deal_list", because you put it (bundle) in the Intent. 因此,首先您应该检查键“ deal_list”,因为您将其(捆绑)放在了Intent中。 and then from bundle fetch the key "jsonArray". 然后从包中获取键“ jsonArray”。

Check your code once. 一次检查您的代码。 Bundle bundle = intent.getParcelableExtra("jsonArray"); 捆绑包bundle = intent.getParcelableExtra(“ jsonArray”);

Here you are doing wrong, directly fetching jsonArray from Intent, instead of first fetching Bundle from intent and then the jsonArray. 在这里,您做错了,直接从Intent中获取jsonArray,而不是先从intent中获取Bundle,然后再从jsonArray中获取。

check this link, see how we use bundle in Intent. 检查此链接,了解我们如何在Intent中使用bundle。 Passing values through bundle and get its value on another activity 通过捆绑包传递值并在另一个活动中获取其值

You can pass json array with simple putExtra, like: 您可以使用简单的putExtra传递json数组,例如:

Intent intent = new Intent(your_activity.this, new_activity.class);
intent.putExtra("jsonArray", mJsonArray.toString());
startActivity(intent);

Where, mJsonArray is your json array. 其中, mJsonArray是您的json数组。

And now, in your new_activity.java : 现在,在您的new_activity.java中

Intent intent = getIntent();
String jsonArray = intent.getStringExtra("jsonArray");

try {
    JSONArray array = new JSONArray(jsonArray);
    System.out.println(array.toString(2));
} catch (JSONException e) {
    e.printStackTrace();
}

I had already tried that and for some reason it didnt start the other activity.我已经尝试过了,但出于某种原因它没有启动其他活动。 Only starts if I remove the putExtra.仅当我删除 putExtra 时才开始。 Thats why I tried the Parcable way这就是我尝试 Parcable 方式的原因

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

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