【发布时间】:2019-05-08 07:26:33
【问题描述】:
我是一个非常新的改造者。我想从服务器获取一些数据。但我无法做到这一点。请帮助我。当我调用 api 时,然后显示一个错误“预期的开始数组但是开始对象”。
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();
}
});
}
【问题讨论】:
-
可能收到的数据格式与您的预期不同。请在此处发布回复。
-
你需要展示 Catagoty 类!
-
@SerializedName("Id") 私有整数 ID; @SerializedName("SubCatagoryDescription") 私有字符串 SubCatagoryDescription; public int getID() { 返回 ID; } 公共字符串 getSubCatagoryDescription() { 返回子类别描述; } public Catagoty(int ID, String subCatagoryDescription) { this.ID = ID;子类别描述 = 子类别描述; }
-
这是我的分类类
标签: android