【发布时间】:2019-03-25 18:01:30
【问题描述】:
在我的应用程序中,我想使用 Retrofit 从服务器获取一些数据。
我写了下面的代码,但是当运行应用程序并调用 api 时,会在下面显示 error :
E/socketLogResponse: Err : com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
请查看我上面的代码并帮助我
来自服务器的 API 响应:
{
"status": "ok",
"time": 0.014972925186157227
}
ApiService 接口:
@POST("api/log")
Call<SocketPingResponse> getSocketPingLog(@Header("jwt") String jwt, @Body SocketPingBodySendData socketPingBodySendData);
SocketPingResponse 类:
public class SocketPingResponse {
@SerializedName("status")
@Expose
private String status;
@SerializedName("time")
@Expose
private Double time;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Double getTime() {
return time;
}
public void setTime(Double time) {
this.time = time;
}
}
SocketPingBodySendData 类:
public class SocketPingBodySendData {
@SerializedName("auction_id")
@Expose
int auction_id;
@SerializedName("data")
@Expose
List<SocketPingEntity> data;
public int getAuction_id() {
return auction_id;
}
public void setAuction_id(int auction_id) {
this.auction_id = auction_id;
}
public List<SocketPingEntity> getData() {
return data;
}
public void setData(List<SocketPingEntity> data) {
this.data = data;
}
}
活动中的 API 调用代码:
pingEntityList.addAll(socketPingDatabase.socketPingDao().getSocketPingEntityList());
SocketPingBodySendData pingBodySendData = new SocketPingBodySendData();
pingBodySendData.setAuction_id(auctionID);
pingBodySendData.setData(pingEntityList);
Toast.makeText(context, ""+pingEntityList.size(), Toast.LENGTH_SHORT).show();
Call<SocketPingResponse> pingResponseCall = apis.getSocketPingLog(jwtToken, pingBodySendData);
pingResponseCall.enqueue(new Callback<SocketPingResponse>() {
@Override
public void onResponse(Call<SocketPingResponse> call, Response<SocketPingResponse> response) {
if (response.body() != null) {
Toast.makeText(context, response.body().getStatus(), Toast.LENGTH_SHORT).show();
if (response.body().getStatus().equals("ok")) {
pingEntityList.clear();
socketPingDatabase.socketPingDao().deleteAll();
}
}
}
@Override
public void onFailure(Call<SocketPingResponse> call, Throwable t) {
Log.e("socketLogResponse", "Err : " + t.toString());
}
});
我该如何解决这个问题?
【问题讨论】:
-
你能发布这个字符串响应吗?有些东西告诉我服务器响应中的问题。