Java 无法从get Reformation Android的response.body()中获取列表
我正在尝试从改造的get请求中获取列表。但是,每当我尝试从response.body()的列表值中访问该值时(它成功地从数据库检索到数据),错误总是索引越界,列表返回null 我正在尝试用列表中的所有数据填充我的RecycleView。然而,它总是返回null,因此我的RecycleView总是空的 下面是我的服务类的代码:Java 无法从get Reformation Android的response.body()中获取列表,java,android,list,null,indexoutofboundsexception,Java,Android,List,Null,Indexoutofboundsexception,我正在尝试从改造的get请求中获取列表。但是,每当我尝试从response.body()的列表值中访问该值时(它成功地从数据库检索到数据),错误总是索引越界,列表返回null 我正在尝试用列表中的所有数据填充我的RecycleView。然而,它总是返回null,因此我的RecycleView总是空的 下面是我的服务类的代码: @GET(“products/getallproducts”)调用getallproducts() 类检索我的列表并填充RecycleView: 公共类HomeActivi
@GET(“products/getallproducts”)调用getallproducts()代码>
类检索我的列表并填充RecycleView:
公共类HomeActivity扩展了AppCompatActivity{
List proGet=new ArrayList();
//这里还有一些代码
//这里还有一些代码
//这里还有一些代码
//最后是方法createDummyData()
public void createDummyData(){
Call Call=restServices.getService().getAllProducts();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
int statusCode=response.code();
productGet=response.body();
//^^^这就是问题所在
//用于填充一个节的类
SectionDataModel dm=新SectionDataModel();
dm.setHeaderTitle(“趋势”);
//类SingleItemModel用于将单个项添加到节中
ArrayList singleItem=新的ArrayList();
//将每个项添加到SingleItem数组列表中
添加(新的SingleItemModel(proGet.get(0.status,productList.get(0.category));
添加(新的SingleItemModel(proGet.get(0.name,productList.get(0.productid));
//将singleItem数组列表填充到一个节中
dm.setAllItemsInSection(单个项目);
//填充整个RecycleView
allSampleData.add(dm);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
`
logcat中的错误如下:
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.kennethlo.kickgoo/com.example.kennethlo.kickgoo.HomeActivity}:java.lang.IndexOutOfBoundsException:索引0无效,大小为0
我很绝望,希望有人能帮助我。提前谢谢。首先需要检查您的响应大小是否大于0(零)
并检查productList的大小是否大于0(零)。onResponse方法中返回的proGet大小为25,但是当我在onResponse方法外部调用proGet时,仍然在createDummyData()内,proGet size返回值为0,并给出索引越界错误。我想知道为什么。当我尝试在SingleItemModel ArrayList中添加项时,请检查productGet和productList的大小是否都大于零proList返回25,proGet在onResponse()内时返回25;但是,当我在onResponse()外调用proGet时,它在onResponse中返回0proGet initialize的大小,因此很明显,在没有初始化的情况下,它的值为零。在将response.body()
分配给productGet
时,它没有任何对象,但在下面的代码中引用proGet
?是的,我需要在onResponse()之外使用proGet.我需要在我声明的另一个方法中使用proGet
public void createDummyData() {
Call<List<Product>> call = restServices.getService().getAllProducts();
call.enqueue(new Callback<List<Product>>() {
@Override
public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {
int statusCode = response.code();
productGet = response.body();
//^^^this is where the problem is
//class used for populating one section
SectionDataModel dm = new SectionDataModel();
dm.setHeaderTitle("trend");
//class SingleItemModel is for adding single item into the section
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
//adding each item into the SingleItem array list
singleItem.add(new SingleItemModel(proGet.get(0).status, productList.get(0).category));
singleItem.add(new SingleItemModel(proGet.get(0).name, productList.get(0).productid));
//populate the singleItem array list into one section
dm.setAllItemsInSection(singleItem);
//populate the whole RecycleView
allSampleData.add(dm);
}
@Override
public void onFailure(Call<List<Product>> call, Throwable t) {
}
});
}
if(productGet.size() > 0){
//Code...
}