我无法使用Retrofit2 + Gson正确解析Web服务的响应。
我已经实施了类似的回复,并且所有回复均正常工作。当响应主体返回自定义对象的数组时,会发生此问题。

我已经阅读了很多来自stackoverflow的回复,并且我使用http://www.jsonschema2pojo.org/创建了类,但是没有办法...

我粘贴了所有代码。希望您能够帮助我。谢谢!

Web服务响应

    {
    "code": "SUCCESS",
    "errorMessage": null,
    "data": [
        {
            "nickname": "David",
            "tiempoMiliseg": 49745,
            "fecha": "2017-07-14T13:59:12.000Z"
        },
        {
            "nickname": "kart 13",
            "tiempoMiliseg": 54034,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 54728,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 16",
            "tiempoMiliseg": 54845,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "jonathan",
            "tiempoMiliseg": 55061,
            "fecha": "2017-07-14T19:12:57.000Z"
        },
        {
            "nickname": "kart 20",
            "tiempoMiliseg": 55187,
            "fecha": "2017-07-14T17:50:51.000Z"
        },
        {
            "nickname": "kart 5",
            "tiempoMiliseg": 55584,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 5",
            "tiempoMiliseg": 55926,
            "fecha": "2017-07-14T15:21:30.000Z"
        },
        {
            "nickname": "kart 19",
            "tiempoMiliseg": 56025,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 11",
            "tiempoMiliseg": 56403,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 22",
            "tiempoMiliseg": 56458,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 28",
            "tiempoMiliseg": 56467,
            "fecha": "2017-07-14T18:10:08.000Z"
        },
        {
            "nickname": "kart 2",
            "tiempoMiliseg": 56548,
            "fecha": "2017-07-14T18:10:08.000Z"
        },
        {
            "nickname": "kart 27",
            "tiempoMiliseg": 56620,
            "fecha": "2017-07-14T15:51:53.000Z"
        },
        {
            "nickname": "kart 27",
            "tiempoMiliseg": 56629,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 20",
            "tiempoMiliseg": 56827,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 56846,
            "fecha": "2017-07-14T18:56:31.000Z"
        },
        {
            "nickname": "kart 28",
            "tiempoMiliseg": 57071,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 26",
            "tiempoMiliseg": 57088,
            "fecha": "2017-07-14T18:34:01.000Z"
        },
        {
            "nickname": "kart 29",
            "tiempoMiliseg": 57143,
            "fecha": "2017-07-14T18:34:01.000Z"
        }
    ]
}


改造响应等级

public class RankingResponse {

@SerializedName("code")
@Expose
private String code;
@SerializedName("errorMessage")
@Expose
private Object errorMessage;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public Object getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(Object errorMessage) {
    this.errorMessage = errorMessage;
}

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

@Override
public String toString() {
    return "RankingResponse{" +
            "code='" + code + '\'' +
            ", errorMessage=" + errorMessage +
            ", data=" + data +
            '}';
}


public class Datum {
    @SerializedName("nickname")
    @Expose
    private String nickname;
    @SerializedName("tiempoMiliseg")
    @Expose
    private Integer tiempoMiliseg;
    @SerializedName("fecha")
    @Expose
    private String fecha;

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Integer getTiempoMiliseg() {
        return tiempoMiliseg;
    }

    public void setTiempoMiliseg(Integer tiempoMiliseg) {
        this.tiempoMiliseg = tiempoMiliseg;
    }

    public String getFecha() {
        return fecha;
    }

    public void setFecha(String fecha) {
        this.fecha = fecha;
    }

    @Override
    public String toString() {
        return "Datum{" +
                "nickname='" + nickname + '\'' +
                ", tiempoMiliseg=" + tiempoMiliseg +
                ", fecha='" + fecha + '\'' +
                '}';
    }
}


}

改造客户

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}


改造响应

java -  retrofit 无法正确解析车身-LMLPHP

最佳答案

在您的RankResponse类中,替换以下内容:

@SerializedName("data")
@Expose
private List<Datum> data = null;


与:

@SerializedName("data")
private List<Datum> data =  new ArrayList<Datum>();

关于java - retrofit 无法正确解析车身,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47629413/

10-10 01:06