如何在<Double>Android中使用Parcelable阅读List<List<List>



在GET中检索信息时,我得到了正确的响应。现在,我正在尝试使用Post调用将一些数据发送回服务器。但当我这样做的时候,我会得到classCastException

这是型号等级

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;
}
}

这是我试图检索并稍后将其发回的json。

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

为此,我得到了java.lang.ClassCastException: java.lang.Double cannot be cast to java.util.Collection

我尝试将它转换为单独的类以将其用作CREATOR,但GET调用失败。

任何帮助都会很棒!

您可能需要实现一个专用的ArrayList,即Parcelable才能工作。如何为包含List<列表<字符串>gt;?这里接受的答案是好的,但你可能想使用泛型,这样它就可以不仅仅用于String或在你的例子中用于Double

顺便说一句,你可能想研究一个单层解决方案,为你协调这样的东西。

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

如果你能做到这一点,你就可以完全避免上述问题。同样,我对你的设置一无所知,我可能完全错了,但嵌套的List并不是世界上最漂亮的东西。

相关内容

最新更新