编译期间缺少JSON响应变量

时间:2018-10-05 09:00:11

标签: android

我正在设置recyclerview以显示来自服务器的一些数据,这就是我的jsonpojo的样子:

public class JSON {

    public class MainCard {
        @SerializedName("Cards")
        public List<Cards> cards;
    }

    public class Cards {
        @SerializedName("Title")
        public String title;
        @SerializedName("Items")
        public List<ItemData> items;
    }

    public class ItemData {
        @SerializedName("Name")
        public String name;
        @SerializedName("Thumb")
        public String thumb;
        @SerializedName("Link")
        public String link;
    }
}

这是适配器:

public class API_Adpater extends RecyclerView.Adapter<API_Adpater.CardsHolder> {
    private List<JSON.ItemData> mlist;
    private List<JSON.Cards> mCards;
    private Context mcontext;

    public API_Adpater(List<JSON.ItemData> mlists, List<JSON.Cards> titles, Context context) {
        this.mlist = mlists;
        this.mcontext = context;
        this.mCards = titles;
    }

    @NonNull
    @Override
    public CardsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.singleitems,viewGroup,false);
        return new CardsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CardsHolder cardsHolder, int i) {
        final JSON.ItemData singleitem = mlist.get(i);
        final JSON.Cards Title = mCards.get(i);
        cardsHolder.textView.setText(Title.title);
        cardsHolder.textView2.setText(singleitem.name);
        cardsHolder.url = singleitem.thumb;
        Glide.with(this.mcontext).load(cardsHolder.url).into(cardsHolder.imageView);
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class CardsHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        TextView textView2;
        String url;
        CardsHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            textView2 = itemView.findViewById(R.id.textView2);
        }
    }
}

这是主要活动:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(APIService.url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);
        Call<JSON.MainCard> call = service.getCards();

        call.enqueue(new Callback<JSON.MainCard>() {
            @Override
            public void onResponse(Call<JSON.MainCard> call, Response<JSON.MainCard> response) {
                if (response.isSuccessful()) {
                    JSON.MainCard mainCard = response.body();
                    if (mainCard != null && mainCard.cards !=null) {
                        List<JSON.ItemData> ru = mainCard.cards.items;
// Here on Above Line it can't get the mainCard.Cards.items; it is not showing the `.items` in the code;
                        setupRV(ru);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Reposnce Error", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<JSON.MainCard> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Check Internet Connectivity", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupRV(List<JSON.ItemData> list) {
        RecyclerView recyclerView = findViewById(R.id.rv);
        LinearLayoutManager lm = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(lm);
        recyclerView.setAdapter(new API_Adpater(list,this));
    }
} 

在进行改造时,我需要将线设置为 List<JSON.ItemData> ru = mainCard.cards.items; 但由于编码编辑器未获取.items变量而无法正常工作 错误在哪里? 无法设置接收器视图。

这是我的json的样子:

{
  "Cards": [
    {
      "Title": "Title",
      "Items": [
        {
          "Name": "Name",
          "Thumb": "Thumb",
          "Link": "link"
        }
      ]
    },
    {
      "Title": "Title",
      "Items": [
        {
          "Name": "Name",
          "Thumb": "Thumb",
          "Link": "link"
        }
      ]
    }
  ]
} 

这是一个错误,无法从items中找到maincards.cards enter image description here

我需要在“回收者”视图中根据URL,名称和标题设置图像。

3 个答案:

答案 0 :(得分:2)

您正在直接访问此行列表中的项目

List<JSON.ItemData> ru = mainCard.cards.items; 

这里的卡片是一个列表,这就是为什么您不能直接访问项目的原因 获取卡片对象,然后使用它

List<JSON.ItemData> ru  = mainCard.cards.get(index).items;

根据您的要求 您需要迭代

if (mainCard != null && mainCard.cards !=null) {
        List<JSON.ItemData> ru = new ArrayList();
        for(Cards itemCard : mainCard.cards)
        {
            ru.addAll(itemCard.items);
        }
                setupRV(ru);
}

答案 1 :(得分:0)

尝试一下。

 List<MoviesApi.ItemData> ru = mainCard.cards.getItems();

答案 2 :(得分:0)

为每个模型分别设置类,如下所示。

-----------------------------------com.example.Card.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Card {

@SerializedName("Title")
@Expose
public String title;
@SerializedName("Items")
@Expose
public List<Item> items = null;

}
-----------------------------------com.example.Item.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Item {

@SerializedName("Name")
@Expose
public String name;
@SerializedName("Thumb")
@Expose
public String thumb;
@SerializedName("Link")
@Expose
public String link;

}
-----------------------------------com.example.MainCard.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class MainCard {

@SerializedName("Cards")
@Expose
public List<Card> cards = null;

}

要创建这些pojo,我使用了该网站http://www.jsonschema2pojo.org/