Android将JsonArray从一种意图传递到另一种意图 [英] Android passing JsonArray from one intent to another

查看:144
本文介绍了Android将JsonArray从一种意图传递到另一种意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在尝试了一段时间.我正在尝试将JsonArray从一种意图传递到另一种意图.这是我的代码.

Been trying this for a while now. I am trying to pass JsonArray from one intent to another. 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实现Parcelable.

JSonArrayParser implements Parcelable.

在将其传递到Bundle之前,我已经检查了是否按预期用Jsonarray填充了JSonArrayParser.

I have checked that the JSonArrayParser is being populated with the Jsonarray as expected before passing it to the Bundle.

问题是当我从另一个意图上读出它时.我得到了JsonArray的空值.

The issue is when I read it out on the other intent. I get a null back for the JsonArray.

这是我到目前为止所拥有的.

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 = "";
                }
            }
        }

我尝试了很多不同的变体来尝试读取值,但是JSONArray似乎始终为null.

I have tried a lot of different variation to try and read the value out but it seems the JSONArray is always null.

在发布此内容之前已经通过互联网进行了搜索.请有人帮忙,那就好了.

Have searched through the internet before posting this. Please if someone can help that would be great.

谢谢.

我尝试按以下方式将JSonArray作为字符串传递,但这似乎根本没有启动意图.

I have tried passing JSonArray as string as following but that didnt seem to start the intent at all.

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

推荐答案

您可以使用简单的putExtra传递json数组,例如:

You can pass json array with simple putExtra, like:

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

mJsonArray是您的json数组.

Where, mJsonArray is your json array.

现在,在您的 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();
}

这篇关于Android将JsonArray从一种意图传递到另一种意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆