0

If I try to get data from my Retrofit call, its empty. Probably because I use the wrong datatype.Not sure if its an Array or an ArrayList Maybe some one could help me out.

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

Interface :

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

Class :

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

}

Call:

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 gives me a good 200 but response.body is empty. Thank you!

1 Answers1

0

The result is not an array. It is an object.

You can try this:

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

Replace the JSONArray to JsonElement.

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
  • Thanks, that worked like a charm. Any chance you know how to properly parse it into an JSONArray? –  Jan 27 '18 at 01:52
  • You can refer to this, how to iterate on JSON properties. https://stackoverflow.com/questions/10165088/iterate-over-jsonobject-properties – Tenten Ponce Jan 27 '18 at 02:07