简体   繁体   English

无法从GET Retrofit Android的response.body()获取列表

[英]Unable to get List from response.body() from GET Retrofit Android

I am trying to get List from GET request with retrofit. 我正在尝试从GET请求中获取列表进行翻新。 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. 但是,每当我尝试从response.body()的列表值访问该值(成功从数据库检索数据)时,错误始终是索引越界,列表将返回null。

I am trying to populate my RecycleView with all the data from the List. 我正在尝试使用列表中的所有数据填充RecycleView。 However it always return null and hence my RecycleView is always empty. 但是,它始终返回null,因此我的RecycleView始终为空。

Below is the code for my service class: 下面是我的服务类的代码:

@GET("products/getallproducts") Call<List<Product>> getAllProducts();

Class for retrieving my List and populate the RecycleView: 用于检索我的列表并填充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: 像这样的logcat错误:

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. 提前致谢。

First need to check your response size is greater than 0 (Zero). 首先需要检查您的响应大小是否大于0(零)。

if(productGet.size() > 0){
    //Code...
}

And also check the size of productList is greate than 0 (Zero). 并且还要检查productList的大小是否大于0(零)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM