S.P.
S.P.

Reputation: 2564

Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY in GET method

I'm trying to get some data with Retrofit and I'm getting this error. I understand what's the error, but I don't know how to fix it:

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

I've tried to find the answer in other questions here, but I can't find anything like my problem...

My Interface Code:

public interface ApiInterface {
    @GET("api/category")
    Call<Category> getBusinessCategory();

}

My class code to call retrofit:

private Call<Category> mCall;

  mCall = apiService.getBusinessCategory();
        mCall.enqueue(new Callback<Category>() {
            @Override
            public void onResponse(Call<Category> call, Response<Category> response) {

                if (response.isSuccess()) {
                    Log.e(TAG, response.toString());

                } else {
                    Toast.makeText(getApplication(), "No conexion", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Category> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });

And this is the json:

[
  {
    "id": 1,
    "name": "FOOD",
    "imageRef": "v1475594353/categories/a",
    "translate": null
  },
  {
    "id": 2,
    "name": "CAR",
    "imageRef": "v1475594195/categories/b",
    "translate": null
   }
]

Category Class:

public class Category  implements Serializable {
  @SerializedName("category")
    @Expose
    private final static String TAG = "Category";
    private String imageRef = "";
    private Long id;
    private String name;
    private String translate;
    private transient Bitmap image;
... Getters and setters

ApiClient class

public class ApiClient {

    public static final String BASE_URL = "http://xxxxx/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }}

Upvotes: 0

Views: 2273

Answers (2)

savepopulation
savepopulation

Reputation: 11921

Here i edited your code:

public interface ApiInterface {
    @GET("api/category")
    Call<List<Category>> getBusinessCategory();
}

Your api returns Array but you are trying to cast it to a single object.

Edit:

  private Call<List<Category>> mCall;
  mCall = apiService.getBusinessCategory();
        mCall.enqueue(new Callback<List<Category>>() {
            @Override
            public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {

                if (response.isSuccess()) {
                    Log.e(TAG, response.toString());

                } else {
                    Toast.makeText(getApplication(), "No conexion", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Category>> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });

Upvotes: 2

b2mob
b2mob

Reputation: 3449

Problem occurred because your trying to parse array to an object, correct your backend implementation or change your code as below:

Your corrected interface:

public interface ApiInterface {
@GET("api/category")
Call<List<Category>> getBusinessCategory(); }

And variable mCall:

private Call<List<Category>> mCall;

Upvotes: 4

Related Questions