Retrofit - 尝试获取response.body时的JsonSyntaxException

时间:2018-01-21 15:07:01

标签: java android android-studio retrofit retrofit2

所以我试图从我的服务器获取response.body,这是GpsCordinates类型的列表。

从服务器我得到这样的回复:

[
{
"x": 33333,
"y": 333,
"date": 1516532556000
},
{
"x": 3,
"y": 4,
"date": 1516542914000
}
]

请求:

@GET("....")
    Call<List<GpsCordinates>> getGPSCordinatesForDay(); 

我如何得到答复:

Api api = Api.RetrofitInstance.create();
    api.getGPSCordinatesForDay().enqueue(new Callback<List<GpsCordinates>>() {
        @Override
        public void onResponse(Call<List<GpsCordinates>> call, Response<List<GpsCordinates>> response) {
            if (response.isSuccessful()) {
                List<GpsCordinates> gpsCordinates = response.body();
            }
        }
        @Override
        public void onFailure(Call<List<GpsCordinates>> call, Throwable t) {
            Log.d("ERROR", t.toString());
        }
    });

GpsCordinates模特:

 @SerializedName("x")
@Expose
private double x;
@SerializedName("y")
@Expose
private double y;
@SerializedName("date")
@Expose
private Date date;

public double getX() {
    return x;
}

public void setX(double x) {
    this.x = x;
}

public double getY() {
    return y;
}

public void setY(double y) {
    this.y = y;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

当我尝试它时,我得到: D /错误:com.google.gson.JsonSyntaxException:1516532556000

1 个答案:

答案 0 :(得分:3)

使用代替Date。您的服务器发送时间戳(毫秒)。