简体   繁体   中英

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. 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 inte.net 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);

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.

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

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

I had already tried that and for some reason it didnt start the other activity. Only starts if I remove the putExtra. Thats why I tried the Parcable way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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