julianfalcionelli / SimpleRESTClientHandler

The easiest way to make http calls
Apache License 2.0
18 stars 1 forks source link

Cannot pass Serializable objects in bundle while placing API call #5

Open mohitajwani opened 6 years ago

mohitajwani commented 6 years ago

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.

joaquinalegatti commented 6 years ago

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

julianfalcionelli commented 6 years ago

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;
}
mohitajwani commented 6 years ago

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

mohitajwani commented 6 years ago

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

joaquinalegatti commented 6 years ago

@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.

julianfalcionelli commented 6 years ago

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