How to fix Expected BEGIN_OBJECT in Retrofit?

99封情书 提交于 2019-12-02 14:03:12

问题


In my application i want use Retrofit for get some data from server.
I write below codes but when run application and call api show me below error :

E/socketLogResponse: Err : com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

Please see my above codes and help me

API response from server :

{
    "status": "ok",
    "time": 0.014972925186157227
}

ApiService interface :

@POST("api/log")
    Call<SocketPingResponse> getSocketPingLog(@Header("jwt") String jwt, @Body SocketPingBodySendData socketPingBodySendData);

SocketPingResponse class :

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 class :

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 call codes in activity :

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());
                            }
                        });

How can i fix this issue?


回答1:


You need to add a gsonconverter factory before building your api service interface.

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

retrofit.create(apiservice.class)


来源:https://stackoverflow.com/questions/52916173/how-to-fix-expected-begin-object-in-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!