预期的begin数组,但是是begin对象

时间:2019-05-08 07:26:34

标签: android

我的翻新非常新。我想从服务器获取一些数据。但是我做不到。请帮帮我。当我调用api时,向我显示一条错误消息“预期的begin数组,但它是begin对象”。

 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();
        }
    });
}

1 个答案:

答案 0 :(得分:1)

正如您期望的那样,当GSON进行转换时,它期望的是Catagoty的JSON数组,因此它可以将其转换为Catagoty的列表。

您得到的错误是由JSON响应的第一个字符引起的,该字符以{开头表示一个对象,但是您期望[表示一个JSON数组

JSON响应可能如下所示。看来api响应实际上是在返回Catagoty对象而不是列表,或者它可能是包装了Catagoty对象数组的顶级JSON对象

[
  {
		"Id": 1,
		"SubCatagoryDescription": "Description 1"
	},
	{
		"Id": 2,
		"SubCatagoryDescription": "Description 2"
	}
]

相关问题