简体   繁体   中英

How to send large json data through intent in android

I already know how to send data between Activities using Intent, the problem is, my server request returns a ArrayList of Images in string base64, and I notice that a large string crashes the app, there's any other way to do this? This how I usually do :

1 Activity :

String passing_data = new Gson().toJson(user);
Intent intent = new Intent(activity,UserAdsView.class);
intent.putExtra("passed_data",passing_data);
activity.startActivity(intent);

2 Activity :

String target = getIntent().getStringExtra("passed_data");
UserModelView userModelView = new Gson().fromJson(target,UserModelView.class);

It doesn't show any error in Console or something, it just crash, and reopen the the previously activity. Thanks!

Hmmm. What do you mean? are you trying to pass a JSON Object or are you trying to pass an arraylist?

I'm guessing you are in need of a bundle and a serializer. Try something like this.

    // Create your new Intent
    Intent intent = new Intent(activity,UserAdsView.class);
    //create a bundle
    Bundle b = new Bundle();
    //when you add a list of objects (JSON or Otherwise), use a serializer
    b.putSerializable("passed_data", passing_data);

    intent.putExtras(b);
    startActivity(intent);

then in your new activity you need something like this:

    ArrayList<String> target = (ArrayList<String>) b.getSerializable("passed_data");

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