SimpleRESTClientHandler icon indicating copy to clipboard operation
SimpleRESTClientHandler copied to clipboard

Cannot pass Serializable objects in bundle while placing API call

Open mohitajwani opened this issue 6 years ago • 6 comments

I am working on a project with this library. I have an API call that accepts the following payload

{
  "first_name": "Mohit",
  "age": 27,
  "address": {
    "line1": "abc",
    "line2": "def",
    "pincode": 400000
  }
}

So, my User Object here has another Serializable Object called Address. I am unable to pass the model as it is in the Bundle by using the following snippet.

Bundle bundle = new Bundle();
bundle.putString("first_name", user.getFirstName());
bundle.putInt("age", user.getAge());
bundle.putSerializable("address", user.getAddress());

The API always gives 503 since the convertToJSON method in the BundleJSONConverter class gives "Unsupported type" exception always because the recursive call makes the class makes the Setter null as the Address object is not a List<T> or a Bundle.

Can you please fix this? Or suggest a workaround.

mohitajwani avatar Oct 23 '18 11:10 mohitajwani

Hi @mohitajwani why aren't you serializing your user class?

joaquinalegatti avatar Oct 23 '18 11:10 joaquinalegatti

Hi @mohitajwani , as @joaquinlateral you could have a DTO (Data Transfer Object) Class and pass it as parameter instead of using a Bundle object.

For example:

public class UpdateUser {
    @SerializedName("first_name")
    String firstName;
    @SerializedName("age")
    int age;
    @SerializedName("address")
    Address address;
}

julianfalcionelli avatar Oct 23 '18 11:10 julianfalcionelli

@joaquinlateral @julianfalcionelli I am already Serializing my class, but still getting the mentioned issue.

mohitajwani avatar Oct 23 '18 11:10 mohitajwani

The actual code has 2 models to be sent in one Request. Both models are in 2 different classes.

mohitajwani avatar Oct 23 '18 11:10 mohitajwani

@mohitajwani I understand but then there is a problem in how you're sending classes and how your code has been modeled. This is not an issue of the library.

joaquinalegatti avatar Oct 23 '18 11:10 joaquinalegatti

Did u try what I put above?, passing a DTO object instead of using a bundle?

julianfalcionelli avatar Oct 23 '18 11:10 julianfalcionelli