【问题标题】:Retrofit - JsonSyntaxException when trying to get response.body改造 - 尝试获取 response.body 时出现 JsonSyntaxException
【发布时间】:2018-06-30 07:04:34
【问题描述】:

所以我试图从我的服务器获取 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());
        }
    });

Gps坐标模型:

 @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

【问题讨论】:

    标签: java android android-studio retrofit retrofit2


    【解决方案1】:

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

    【讨论】:

    • 在我的服务器中日期也是 java.util.Date 类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 2016-12-23
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多