2

I a very new in retrofit.I want to fetch some data from server.but I am unable to do this. Please help me.when I call api then show me an error "expected begin array but was begin object".

 private void getCatagories(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
            .build();

    Api api = retrofit.create(Api.class);
    Call<List<Catagoty>> call = api.getCatagories();
    Log.e("this",Api.BASE_URL+"");
    call.enqueue(new Callback<List<Catagoty>>() {
        @Override
        public void onResponse(Call<List<Catagoty>> call, Response<List<Catagoty>> response) {
            List<Catagoty> catagotiesList = response.body();
            //Creating an String array for the ListView
            String[] heroes = new String[catagotiesList.size()];

            //looping through all the heroes and inserting the names inside the string array
            for (int i = 0; i < catagotiesList.size(); i++) {
                heroes[i] = catagotiesList.get(i).getSubCatagoryDescription();
            }


            //displaying the string array into listview
            listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));

        }

        @Override
        public void onFailure(Call<List<Catagoty>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}
Arpan Sarkar
  • 189
  • 3
  • 14
  • Probably received data format differ from what you are expecting. Please post response here. – Sebastian Pakieła May 08 '19 at 07:28
  • You need to show Catagoty class! – Nick Bapu May 08 '19 at 07:29
  • @SerializedName("Id") private int ID; @SerializedName("SubCatagoryDescription") private String SubCatagoryDescription; public int getID() { return ID; } public String getSubCatagoryDescription() { return SubCatagoryDescription; } public Catagoty(int ID, String subCatagoryDescription) { this.ID = ID; SubCatagoryDescription = subCatagoryDescription; } – Arpan Sarkar May 08 '19 at 07:30
  • this is my category class – Arpan Sarkar May 08 '19 at 07:30

1 Answers1

1

As you are expecting a List then when GSON does the conversion it is expecting a JSON array of Catagoty so it can convert this to a list of Catagoty.

The error you are getting is caused by the first character of the JSON response starting with a { to denote an object, but you are expecting a [ to denote a JSON array

What the JSON response should probably be is something like below. It seems like the api response is actually returning a Catagoty object and not a list or perhaps it's a top level JSON object which wraps an array of Catagoty object

[
  {
  "Id": 1,
  "SubCatagoryDescription": "Description 1"
 },
 {
  "Id": 2,
  "SubCatagoryDescription": "Description 2"
 }
]
alexy
  • 464
  • 5
  • 6
  • 1
    So does the body of the response start with `[` ? – alexy May 08 '19 at 10:20
  • Can you debug the response and see exactly where the error is thrown? – alexy May 08 '19 at 12:48
  • Also, I just found this post which might be of use. https://stackoverflow.com/questions/36656827/how-to-parse-list-of-json-objects-surrounded-by-using-retrofit-and-gson The OP imported the wrong String type (com.sun.org.apache.xpath.internal.operations.String instead of java.lang.String) which messed up GSONs deserialization. – alexy May 08 '19 at 12:59
  • I know this is not exactly the same error message as yours, but it makes me think that there could be some issue with your Catagoty class. Can you post the whole class including imports and the whole api JSON response? – alexy May 08 '19 at 13:05