0

While retrieving information in GET, I get proper response. Now, I am trying to send some data back to Server using Post call. But when I do it, I get classCastException

Here is model class

public class Polygon implements Parcelable
{

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("coordinates")
    @Expose
    private List<List<List<Double>>> coordinates = new ArrayList<>();
    public final static Creator<Polygon> CREATOR = new Creator<Polygon>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Polygon createFromParcel(android.os.Parcel in) {
            return new Polygon(in);
        }

        public Polygon[] newArray(int size) {
            return (new Polygon[size]);
        }

    };

    protected Polygon(android.os.Parcel in) {
        this.type = ((String) in.readValue((String.class.getClassLoader())));
        in.readList(this.coordinates, (List.class.getClassLoader()));
    }

    public Polygon() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<List<List<Double>>> getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(List<List<List<Double>>> coordinates) {
        this.coordinates = coordinates;
    }

    public void writeToParcel(android.os.Parcel dest, int flags) {
        dest.writeValue(type);
        dest.writeList(coordinates);
    }

    public int describeContents() {
        return 0;
    }

}

Here is the json I am trying to retrieve and send it back later.

{"polygon": {
"type": "Polygon",
"coordinates": [
[
[
-105.18367,
39.54363
],
[
-105.18367,
39.98435
],
[
-104.33773,
39.98435
],
[
-104.33773,
39.54363
],
[
-105.18367,
39.54363
]
     ]
    ]
  }
}

For this I get java.lang.ClassCastException: java.lang.Double cannot be cast to java.util.Collection.

I tried converting it to separate class to use it as CREATOR but then GET call fails.

Any help would be great!

Roon13
  • 387
  • 2
  • 23

1 Answers1

1

You may need to implement a specialize ArrayList that is Parcelable for this to work. How to implement Parcelable for a class containing List<List<String>>? The accepted answer here is good but you may want to use generics so it can used for more than just String or in your instance Double

As a side note you may want to look into a single layer solution for you coordinates something like this.

public class Coordinates implements Parcelable{
    public double x;
    public double y;
    public double z;
    // You get the gist
}
List<Coordinates> coordinates = new ArrayList<>();

If you can do this you could avoid the above problem altogether. Again I have no clue about your setup and I could be completely wrong BUT a nested nested List is not the prettiest thing in the world.

avalerio
  • 2,072
  • 1
  • 12
  • 11
  • I totally agree with nested list but this is what I have from my backend team! But it worked! Additional to your answer I had to extend class to ArrayList just like linked answer! I was close but your support helped me big time! – Roon13 Nov 16 '21 at 04:28