Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I have encountered a problem when I want to fetch my data to recyclerview using Java, and resulting in the problem:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

My API Response :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "id": "1",
        "type": "Barang Elektronik",
        "payment_method": "Cash",
        "price": "500000",
        "date": "2023-07-11 00:00:00",
        "detail": "Charger HP"
    },
    {
        "id": "2",
        "type": "Bahan Makanan",
        "payment_method": "Cash",
        "price": "50000",
        "date": "2023-07-11 02:49:57",
        "detail": "Mie Goreng 1 Dus"
    },
    {
        "id": "3",
        "type": "Kecantikan",
        "payment_method": "Gopay",
        "price": "120000",
        "date": "2023-07-11 00:00:00",
        "detail": "Skincare"
    },
    {
        "id": "5",
        "type": "Lainnya",
        "payment_method": "Debit",
        "price": "1200000",
        "date": "2023-07-11 00:00:00",
        "detail": "Liburan"
    }
]

ApiInterface.java

@GET("api/index.php?route=expenses")
Call<AllExpenseResponse> getAllExpenseItem(@Query("id") String userID);

and the file that I have from generated POJO extension is:
AllExpenseResponse.java

package hafizcaniago.my.id.papb_final.Data.Response.Expenses;

import java.util.List;
import com.google.gson.annotations.SerializedName;

public class AllExpenseResponse{

    @SerializedName("AllExpenseResponse")
    private List<AllExpenseResponseItem> allExpenseResponse;

    public List<AllExpenseResponseItem> getAllExpenseResponse(){
        return allExpenseResponse;
    }
}

AllExpenseResponseItem.java

package hafizcaniago.my.id.papb_final.Data.Response.Expenses;

import com.google.gson.annotations.SerializedName;

public class AllExpenseResponseItem{

    @SerializedName("date")
    private String date;

    @SerializedName("price")
    private String price;

    @SerializedName("id")
    private String id;

    @SerializedName("detail")
    private String detail;

    @SerializedName("type")
    private String type;

    @SerializedName("payment_method")
    private String paymentMethod;

    public String getDate(){
        return date;
    }

    public String getPrice(){
        return price;
    }

    public String getId(){
        return id;
    }

    public String getDetail(){
        return detail;
    }

    public String getType(){
        return type;
    }

    public String getPaymentMethod(){
        return paymentMethod;
    }
}

and this is MainActivity.java in load RecyclerView

RestClient.getService().getAllExpenseItem(USER_ID).enqueue(new Callback<AllExpenseResponse>() {
            @Override
            public void onResponse(Call<AllExpenseResponse> call, Response<AllExpenseResponse> response) {
                if (response.isSuccessful()) {
                    expenseItem = response.body().getAllExpenseResponse();

                    Log.i("responseExpense", expenseItem.toString());

                    adapter = new ExpenseAdapter(expenseItem, getApplicationContext());
                    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                    recyclerView.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<AllExpenseResponse> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Something Wrong, Check Log for Details", Toast.LENGTH_SHORT).show();
                Log.i("error", t.toString());
                Log.i("error", call.toString());
            }
        });

I see some problem like that, but I don’t still get it, what I must do. Please help me.

>Solution :

Change the return type of your api function to :

 @GET("api/index.php?route=expenses")
 Call<List<AllExpenseResponseItem>> getAllExpenseItem(@Query("id") String userID);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading