GSON converting a string value into null

596 Views Asked by At

I send a request up to an API and use GSON to convert it into an object.

I then pass it through a Bundle and pass it as a JSON String.

The problem is that before I pass it through the bundle the value looks fine, but when I receive the bundle in the next activity the value is then Null

What seems to be the problem here? I am guessing that GSON is having some problems parsing out the info.

This is what I see for the string key before I run pass it through the bundle.

http://XXXXXXXX.XXXXXX.com/%7B%7BSIZE%7D%7D-290x116.jpg

This is what I see after I pass it through the bundle

http://XXXXXXXX.XXXXXX.com/null

I using the following code to pass it through the bundle:

Bundle bundle = new Bundle();
        Gson gson = new Gson();
        bundle.putString("my_model", gson.toJson(searchResult, ModelClass.class));
        i.putExtras(bundle);
        startActivity(i);

This is the code I am using to convert the JSON back to an object:

Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String myModelJSON = bundle.getString("my_model");
        Gson gson = new Gson();
        mMyModel = gson.fromJson(myModelJSON, MyModel.class);=
    }

Notice the null at the end

How should I go about fixing this? I am thinking about using Parcelable if nothing pans out.

EDIT:

Response form API using a rest client app:

"image_path": "/uploads/image/image02-%7B%7BSIZE%7D%7D-290x116.jpg"

1

There are 1 best solutions below

0
On BEST ANSWER

The problem seemed to be that the Object I was passing through the bundle contained lists of Objects within itself, it seems like GSON has issues converting that back to its POJO object so I just grabbed the lists and passed those instead like so:

bundle.putString("list", gson.toJson(myModelObject.getList()));

Then in the next activity I grabbed the list like so:

Gson gson = new Gson();

// Get List
Type typeEntityListObject = new TypeToken<List<ListObject>>(){}.getType();
mList = gson.fromJson(bundle.getString("list"), typeEntityListObject);

Now it doesn't return Null anymore but as someone pointed out above the API is returning back a faulty string for the path, at least the stuff is working fine on my end.