Search code examples
androidretrofit2

Expected BEGIN_OBJECT but was BEGIN_ARRAY but the json respone has already an object


I am learning retrofit 2 and got an error like this :

W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 94 path $.riceField at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)

and here is the code

api

@GET(Server.riceFields+"/{id}")
Call<Product> showRiceFields(@Path("id") int id);

model

public class Product {

    private Status status;
    private RiceField riceField;

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public RiceField getRiceField() {
        return riceField;
    }

    public void setRiceField(RiceField riceField) {
        this.riceField = riceField;
    }


}

and

ProductsEndPoint.productsEndPoint().showRiceFields(id)
            .enqueue(new Callback<Product>() {
                @Override
                public void onResponse(Call<Product> call, Response<Product> response) {
                    if (response.isSuccessful()) {
                        RiceField riceField = response.body().getRiceField();
                        product = riceField;
                        setView(product);
                    }
                }

                @Override
                public void onFailure(Call<Product> call, Throwable t) {
                    t.printStackTrace();
                }
            });

json respone

{
    "status": {
        "code": 200,
        "message": "Succes",
        "description": "Data berhasil diambil"
    },
    "riceField": [
        {
            "id": 21,
            "title": "Nihil qui placeat tempora magnam aut omnis quae alias aliqua Molestiae culpa",
            "harga": 45,
            "luas": 20,
            "alamat": "Ratione cum et aliqu",
            "maps": "Quia aut ut dolores nisi hic necessitatibus asperiores odio dolores ut non voluptatum ipsum enim id",
            "deskripsi": "Voluptatum repellend",
            "sertifikasi": "sgb",
            "tipe": "jual",
            "created_at": "2021-06-08T08:11:00.000000Z",
            "updated_at": "2021-06-08T08:11:00.000000Z",
            "user_id": 2,
            "vestige_id": 2,
            "irrigation_id": 3,
            "region_id": 3,
            "verification_id": 2
        }
    ]
}

i have add List on the callback like this <List<Product>> and the error turn to

BEGIN_ARRAY but was BEGIN_OBJECT

so, what should i do?

thanks and sorry for my bad english

the solution : i should add List on model every array json


Solution

  • You are using the wrong model. according to your json output, your model must be the below classes

    public class Output{
    
    public Status status;
    public List<RiceField> riceField;
    
    }
    
    
    public class RiceField {
    
    public Integer id;
    public String title;
    public Integer harga;
    public Integer luas;
    public String alamat;
    public String maps;
    public String deskripsi;
    public String sertifikasi;
    public String tipe;
    public String createdAt;
    public String updatedAt;
    public Integer userId;
    public Integer vestigeId;
    public Integer irrigationId;
    public Integer regionId;
    public Integer verificationId;
    
    }
    
    
    public class Status {
    
    public Integer code;
    public String message;
    public String description;
    
    }
    

    for more information use this website : json to java class