【问题标题】:Retrofit - JSON Array problems改造 - JSON 数组问题
【发布时间】:2018-01-27 01:33:23
【问题描述】:

如果我尝试从我的 Retrofit 调用中获取数据,它是空的。可能是因为我使用了错误的数据类型。不确定它是 Array 还是 ArrayList 也许有人可以帮助我。

API:https://chasing-coins.com/api/v1/top-coins/100

界面:

 @GET("api/v1/top-coins/{top}")
    Call<JSONArray> getStats(@Path("top")String top);

类:

public class Coinstats {

    @SerializedName("symbol")
    @Expose
    private String symbol;
    @SerializedName("cap")
    @Expose
    private String cap;
    @SerializedName("change")
    @Expose
    private Change change;
    @SerializedName("price")
    @Expose
    private String price;
    @SerializedName("coinheat")
    @Expose
    private Integer coinheat;
    @SerializedName("url")
    @Expose
    private String url;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getCap() {
        return cap;
    }

    public void setCap(String cap) {
        this.cap = cap;
    }

    public Change getChange() {
        return change;
    }

    public void setChange(Change change) {
        this.change = change;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Integer getCoinheat() {
        return coinheat;
    }

    public void setCoinheat(Integer coinheat) {
        this.coinheat = coinheat;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}

呼叫:

call2.enqueue(new Callback<JSONArray>() {
            @Override
            public void onResponse(Call<JSONArray> call, Response<JSONArray> response) {

                Log.w("Stats", response.message());

            }
            @Override
            public void onFailure(Call<JSONArray> call, Throwable t) {
                Log.w("no Stats", t.toString());
            }
        });

Respones Message 给了我一个很好的 200 但 response.body 是空的。 谢谢!

【问题讨论】:

  • 结果不是数组。它是一个对象。

标签: android json retrofit retrofit2


【解决方案1】:

结果不是数组。它是一个对象。

你可以试试这个:

@GET("api/v1/top-coins/{top}")
Call<JsonElement> getStats(@Path("top")String top);

JSONArray 替换为JsonElement

【讨论】:

猜你喜欢
  • 2015-11-16
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 2018-03-19
相关资源
最近更新 更多