Android passing JsonArray from one intent to anoth

2019-09-08 14:55发布

问题:

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 implements Parcelable.

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

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

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.

Thanks in advance.

EDIT:

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);

回答1:

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. directly like above code.



回答2:

I think you have used wrong keys... Please check..

You are putting bundle in Intent with key "deal_list". and in bundle you have jsonarray with key "jsonArray".

So first u should check for key "deal_list", because you put it (bundle) in the Intent. and then from bundle fetch the key "jsonArray".

Check your code once. Bundle 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.

check this link, see how we use bundle in Intent. Passing values through bundle and get its value on another activity



回答3:

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);

Where, mJsonArray is your json array.

And now, in your 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();
}