0

I cannot get response. Every time it says Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 11 path $.total. I am new in Android. I have tried lots of solutions but not worked. Here is the response from server -

{
"total": {
"confirmed": 38292,
"recovered": 7925,
"deaths": 544,
"tested": 266509
},
"last": {
"tested": "8015",
"confirmed": "1541",
"recovered": "346",
"deaths": "22"
},
"lastUpdate": "Wednesday, 27 May, 2020 03:32PM"
}

Here is my model -

public class Stat {
private List<Total> total;
private List<Last> last;
private String lastUpdate;

public List<Total> getTotal() {
    return total;
}

public void setTotal(List<Total> total) {
    this.total = total;
}

public List<Last> getLast() {
    return last;
}

public void setLast(List<Last> last) {
    this.last = last;
}

public String getLastUpdate() {
    return lastUpdate;
}

public void setLastUpdate(String lastUpdate) {
    this.lastUpdate = lastUpdate;
}
}

Here is my activity -

ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<Stat> call = apiInterface.getStats();
    call.enqueue(new Callback<Stat>() {
        @Override
        public void onResponse(Call<Stat> call, Response<Stat> response) {
            progressDialog.dismiss();
            if (response.isSuccessful()) {
                int code = response.code();
                Log.i("code ", Integer.toString(code));
            }
        }

        @Override
        public void onFailure(Call<Stat> call, Throwable t) {
            progressDialog.dismiss();
            Log.i("error ", t.getMessage());
        }
    });

Here is my interface -

@GET("stats")
Call<Stat> getStats();
  • 1
    Does this answer your question? [Expected BEGIN\_ARRAY but was BEGIN\_OBJECT retrofit2](https://stackoverflow.com/questions/47346823/expected-begin-array-but-was-begin-object-retrofit2) – Andre Classen May 27 '20 at 17:17

1 Answers1

1

In your model, total is a List, but in the response you are getting a single JSON object for total. You should convert it into a single Total object, instead of List. Or, you can make change in the sever, to return a list of objects, instead of a single object.

Looking at your code, you should do the same for 'last' as well.

In the future you can use this: http://www.jsonschema2pojo.org/ to convert JSON to JAVA POJO class.

Vansh Arora
  • 392
  • 2
  • 13