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

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

    }

}



跃然一笑
浏览 221回答 3
3回答

扬帆大鱼

Retrofit 是类型安全库。这意味着它只等待特定(预定义)类型的对象。如果服务器发送其他内容 - 它会因错误而崩溃。这是你的情况。只需检查原始服务器响应,您就会发现有什么问题。

动漫人物

我认为当没有数据返回时问题会返回字符串。后端通常会出现此类错误。这个错误以前发生在我身上。当没有数据可用时,您应该检查响应 json

拉风的咖菲猫

尝试更改您的 API 调用从@POST("api/log")Call<SocketPingResponse> getSocketPingLog(@Header("jwt") String jwt, @Body SocketPingBodySendData socketPingBodySendData);到@POST("api/log")Call<String> getSocketPingLog(@Header("jwt") String jwt, @Body SocketPingBodySendData socketPingBodySendData);pingEntityList.addAll(socketPingDatabase.socketPingDao().getSocketPingEntityList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SocketPingBodySendData pingBodySendData = new SocketPingBodySendData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pingBodySendData.setAuction_id(auctionID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pingBodySendData.setData(pingEntityList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, ""+pingEntityList.size(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Call<String> pingResponseCall = apis.getSocketPingLog(jwtToken, pingBodySendData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pingResponseCall.enqueue(new Callback<String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(Call<String> call, Response<String> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.body() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Convert here your string response to Other POJO format&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<String> call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("socketLogResponse", "Err : " + t.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java