在我的应用程序中,我想使用Retrofit从服务器获取一些数据.
我写下面的代码,但是当运行应用程序并调用API时,显示以下错误:
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()); } });
我该如何解决这个问题?
解决方法:
您需要在构建API服务接口之前添加gsonconverter工厂.
Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build();retrofit.create(APIservice.class)
总结 以上是内存溢出为你收集整理的java-如何在翻新中修复预期的BEGIN_OBJECT?全部内容,希望文章能够帮你解决java-如何在翻新中修复预期的BEGIN_OBJECT?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)