LaVOZs

The World’s Largest Online Community for Developers

'; android - Invalid network request with retrofit - LavOzs.Com

Sorry I know the question does't make sense, but I just can't figure out how to ask the question. I'm using model class that extend RealmObject, serialization by Gson. When I make a request, I send this json:

{"clothing_item_ids": [],
  "date": "2020-01-17 02:19:41 -0500",
  "location": {
   "id": 0
  },
"name": "Good event",
 "tag_ids": []
}

But I want to send it without the location object when location is null. I can't figure out if the problem came from the way I implemented the Realm, Please help and excuse my ignorance on the matter.

@Parcel(implementations = {LocationRealmProxy.class}, value = Parcel.Serialization.FIELD, analyze = {Location.class})
public class Location extends RealmObject {

@PrimaryKey
@SerializedName("id")
public int id;
@SerializedName("city")
public String city;
@SerializedName("postal_code")
public String postalCode;
@SerializedName("state")
public String state;
@SerializedName("street")
public String street;

public Location() {
}

public Location(Location location) {
    this.id = location.id;
    this.city = location.city;
    this.postalCode = location.postalCode;
    this.state = location.state;
    this.street = location.street;
}
}

Here's the event class:

@Parcel(implementations = {EventRealmProxy.class}, value = Parcel.Serialization.FIELD, analyze = {Event.class})
public class Event extends RealmObject implements Serializable {

public static final String DATE_FIELD = "timestamp";
@PrimaryKey
@SerializedName("id")
public String id;
@SerializedName("date")
public String date;
@Nullable
public Date timestamp;
@SerializedName("image")
public String image;
@SerializedName("name")
public String name;

@Nullable
@SerializedName("event_clothing_items")
@ParcelPropertyConverter(RealmListParcelConverter.class)
public RealmList<ClothingItem> eventClothingItems;

@Nullable
@SerializedName("location")
public Location location;

@Nullable
@SerializedName("tags")
@ParcelPropertyConverter(RealmListParcelConverter.class)
public RealmList<Tag> tags;

public Event() {
    timestamp = new Date();
    eventClothingItems = new RealmList<>();
    location = new Location();
    tags = new RealmList<>();
}

public Event(Event event) {
    eventClothingItems = new RealmList<>();
    location = new Location();
    tags = new RealmList<>();
    this.id = event.id;
    this.date = event.date;
    this.image = event.image;
    this.name = event.name;
    this.tags.addAll(event.tags);
    this.eventClothingItems.addAll(event.eventClothingItems);
    this.location = new Location(event.location);

}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Event event = (Event) o;
    return id.equals(event.id);
}

@Override
public int hashCode() {
    return Objects.hash(id);
}
}

This is the Request model

public class CreateEventRequestModel  {
public String name;
public String date;
public Location location;
@SerializedName("clothing_item_ids")
public List<Integer> clothingItemIds;
@SerializedName("tag_ids")
public List<Integer> tagIds;

private CreateEventRequestModel() {
}

public CreateEventRequestModel(Event event) {
    if (event == null) {
        return;
    }
    this.name = event.name;
    this.date = event.date;
    this.location = event.location;
    this.tagIds = new ArrayList<>();
    this.clothingItemIds = new ArrayList<>();
    if (event.tags != null) {
        for (Tag tag : event.tags) {
            this.tagIds.add(tag.id);
        }
    }

    if (event.eventClothingItems != null) {
        for (ClothingItem clothingItem : event.eventClothingItems) {
            this.clothingItemIds.add(Integer.valueOf(clothingItem.id));
        }
    }
}
}

Help me please figure out what's the problem here, why can't location have all his field as null, or how can I check when location is valid(field should not be empty, or all field should be null or remove location object).

Related
Why does my exception handler not trap Android SQLite insert error?
How to POST raw whole JSON in the body of a Retrofit request?
How to log request and response body with Retrofit-Android?
How to handle Dynamic JSON in Retrofit?
How to set timeout in Retrofit library?
Unable to create converter for my class in Android Retrofit library
Logging with Retrofit 2
Retrofit 2 - Dynamic URL
How to implement parcelable with my custom class containing Hashmap and SparseArray?