Json 使用 RetroFit 将对象放入数组列表 Android
Json object into array list with RetroFit Android
我正在尝试从休息中获取此数据 API 并显示在 Recycler 视图中。我尝试将 JSON 对象转换为 ArrayList,但没有成功。我的代码 returns 这个:[]
目前,我正在使用 Retrofit 和 POJO 类。
//Header
ArrayList<Model> carsModels=new ArrayList<>();
private Adapter carsAdapter;
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInteface requestInteface = retrofit.create(RequestInteface.class);
Call<Model> call = requestInteface.getInteressJson();
call.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> 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();
}
}
});
型号Class:
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
请求接口:
interface RequestInteface {
@GET("search.json")
Call<Model> getInteressJson();
}
你需要先解析"data",在"data"里面包含List<Model>
创建classInteressResponse
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();
}
}
删除class里面的private List<Model> modelList = new ArrayList<>();
Model
我正在尝试从休息中获取此数据 API 并显示在 Recycler 视图中。我尝试将 JSON 对象转换为 ArrayList,但没有成功。我的代码 returns 这个:[] 目前,我正在使用 Retrofit 和 POJO 类。
//Header
ArrayList<Model> carsModels=new ArrayList<>();
private Adapter carsAdapter;
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInteface requestInteface = retrofit.create(RequestInteface.class);
Call<Model> call = requestInteface.getInteressJson();
call.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> 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();
}
}
});
型号Class:
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
请求接口:
interface RequestInteface {
@GET("search.json")
Call<Model> getInteressJson();
}
你需要先解析"data",在"data"里面包含List<Model>
创建classInteressResponse
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();
}
}
删除class里面的private List<Model> modelList = new ArrayList<>();
Model