Skip to content Skip to sidebar Skip to footer

How To Pass Json Object Data Using Gson?

i know there is lots of solution available but none of them help me i am parsing json data data using Gson, and i want to parse this data to another activity as well but i am getti

Solution 1:

Gsongson=newGson();
MyProfileobj=newMyProfile();
StringjsonInString= gson.toJson(obj);
// pass string object to next activity and convert string object to class.//next activity write this code.
MyProfile obj= gson.fromJson(jsonInString, MyProfile.class);

Solution 2:

Make your MyProfile.class implement Serializable

make sure your response is in json format and then in onResponse() of volley use GsonBuilder() like this

MyProfile myprofile = newGsonBuilder().create().fromJson(response.toString(), MyProfile .class);

then start activity with intent

    Intent i = newIntent(this, NextActivity.class);
i.putExtra("myprofileObject", myprofile );
startActivity(i);

and receive the object in NextActivity.class like this

Intentintent= getIntent();
MyProfilemyprofile= (MyProfile )intent .getSerializableExtra("myprofileObject");

If you implement your class as Parcelable then receive the object in NextActivity.class like this

Intentintent= getIntent();
    MyProfilemyprofile= (MyProfile )intent .getParcelableExtra("myprofileObject");

Solution 3:

Instead of passing MyProfile Object as Parcelable, try as below in the sender class:

Stringdata=newGson().toJson(mMyProfile);

Intentintent=newIntent(getBaseContext(), EditProfileActivity.class);
            intent.putExtra("profile ", data);
            startActivity(intent);

At Receiver class, get your profile info as below:

String data = getIntent().getStringExtra("profile");

MyProfile object = new Gson().fromJson(data,MyProfile.class)

This might work for your case.

Post a Comment for "How To Pass Json Object Data Using Gson?"