繁体   English   中英

如何在改造中修复预期的BEGIN_OBJECT?

[英]How to fix Expected BEGIN_OBJECT in Retrofit?

在我的应用程序中,我想使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM