0

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);
dogwasstar
  • 852
  • 3
  • 16
  • 31

3 Answers3

0

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.

uma
  • 1,477
  • 3
  • 32
  • 63
  • Not sure if you read my post correctly but I can pass string between intent / activity just fine. Its when I try to pass in JSonArray it seems to not work correctly. Thanks tho – dogwasstar Oct 04 '15 at 03:15
0

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

Community
  • 1
  • 1
Harish Vats
  • 662
  • 6
  • 21
0

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();
}
Rahul Parihar
  • 640
  • 7
  • 16
  • 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 – dogwasstar Oct 05 '15 at 00:48