I am trying to get List from GET request with retrofit. But whenever I try to access the value from the list value from the response.body() (which is successful to retrieve the data from database),the error is always index out of bound, which the List returns null.
I am trying to populate my RecycleView with all the data from the List. However it always return null and hence my RecycleView is always empty.
Below is the code for my service class:
@GET("products/getallproducts") Call<List<Product>> getAllProducts();
Class for retrieving my List and populate the RecycleView:
public class HomeActivity extends AppCompatActivity {
List<Product> proGet = new ArrayList<Product>();
//some more codes here
//some more codes here
//some more codes here
//lastly followed by the method createDummyData()
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) {
}
});
}
`
Error in logcat as such:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kennethlo.kickgoo/com.example.kennethlo.kickgoo.HomeActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
I am desperate and hoping someone able to help. Thanks in advance.