通过改造Android将Json对象放入数组列表

通过改造Android将Json对象放入数组列表,android,json,api,retrofit,Android,Json,Api,Retrofit,我试图从RESTAPI中获取这些数据,并显示在Recycler视图中。我尝试将JSON对象转换为ArrayList,但没有成功。我的代码返回这个:[] 目前,我正在对POJO类进行改造 //头 ArrayList carsModels=新的ArrayList(); 专用适配器车载适配器; 改装改装=新改装.Builder() .baseUrl(“http://example.com/") .addConverterFactory(GsonConverterFactory.create()) .

我试图从RESTAPI中获取这些数据,并显示在Recycler视图中。我尝试将JSON对象转换为ArrayList,但没有成功。我的代码返回这个:[] 目前,我正在对POJO类进行改造

//头
ArrayList carsModels=新的ArrayList();
专用适配器车载适配器;
改装改装=新改装.Builder()
.baseUrl(“http://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInteface RequestInteface=reformation.create(RequestInteface.class);
Call Call=requestInteface.getInterestJSON();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
carsModels=newarraylist(response.body().getModelList());
carsAdapter=新适配器(home.this,carsModels);
cars_recyclerview.setAdapter(carsAdapter);
Toast.makeText(home.this,“Success”,Toast.LENGTH_SHORT).show();
}
}
});
模型类:

public class Model {
@SerializedName("name")
@Expose
private String name;
@SerializedName("audience_size")
@Expose
private String audience_size;
@SerializedName("topic")
@Expose
private String topic;
@SerializedName("path")
@Expose
private String path;
@SerializedName("description")
@Expose
private String description;

private List<Model> modelList = new ArrayList<>();


//Getter and setter
公共类模型{
@序列化名称(“名称”)
@暴露
私有字符串名称;
@SerializedName(“受众大小”)
@暴露
私有字符串大小;
@序列化名称(“主题”)
@暴露
私有字符串主题;
@序列化名称(“路径”)
@暴露
私有字符串路径;
@序列化名称(“说明”)
@暴露
私有字符串描述;
private List modelList=new ArrayList();
//接二连三
请求接口:

interface RequestInteface {
@GET("search.json")
Call<Model> getInteressJson();
}
接口请求接口{
@获取(“search.json”)
调用getInterestsJSON();
}
您需要首先解析“数据”,在“数据”中包含
列表
创建类
兴趣响应

public class InteressResponse {
@SerializedName("data")
@Expose
private List<Model> modelList;

//Getter and setter
}
公共类兴趣响应{
@SerializedName(“数据”)
@暴露
私有列表模型列表;
//接二连三
}
接口请求接口{
@获取(“search.json”)
调用getInterestsJSON();
}
public void onResponse(调用、响应){
if(response.issusccessful()){
carsModels=newarraylist(response.body().getModelList());
carsAdapter=新适配器(home.this,carsModels);
cars_recyclerview.setAdapter(carsAdapter);
Toast.makeText(home.this,“Success”,Toast.LENGTH_SHORT).show();
}
}

Remove
private List modelList=new ArrayList();
inside class
Model

返回此错误:尝试对空对象引用调用接口方法“java.lang.Object[]java.util.Collection.toArray()”
public class InteressResponse {
@SerializedName("data")
@Expose
private List<Model> modelList;

//Getter and setter
}
interface RequestInteface {
@GET("search.json")
Call<InteressResponse> getInteressJson();
}
public void onResponse(Call<InteressResponse> call, Response<InteressResponse> response) {
      if(response.isSuccessful()){
          carsModels = new ArrayList<>(response.body().getModelList()); 
          carsAdapter = new Adapter(home.this, carsModels);
          cars_recyclerview.setAdapter(carsAdapter);
          Toast.makeText(home.this,"Success",Toast.LENGTH_SHORT).show();
      }
}