我有一个带嵌套对象的JSON url。第一类是SubNodes,我可以从中获取数据。它还有一个标题为ImageList的不同对象保存图像但我无法从ImageList获取数据。我正在使用RecyclerView。 Log返回null。我该如何解决这个错误?
SubNodes.java
public class SubNodes {
public String body;
public ImageList image;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
ImageList.java
public class ImageList {
public String src;
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
改造2代码
requestCatalog.enqueue(new Callback<NewsCatalog>() {
@Override
public void onResponse(Call<NewsCatalog> call, Response<NewsCatalog> response) {
NewsCatalog catalog = response.body();
SubNodes subNodes;
mainNodes = new MainNodes();
try {
for (int i = 0; i < catalog.nodes.size(); i++) {
mainNodes = catalog.nodes.get(i);
subNodes = new SubNodes();
subNodes.setBody(mainNodes.node.body);
Toast.makeText(context,mainNodes.node.image.src,Toast.LENGTH_SHORT).show();
subNodes.image.setSrc("bxdbsnd");
subNodesList.add(subNodes);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.e(TAG, "" + e.getCause());
}
}
@Override
public void onFailure(Call<NewsCatalog> call, Throwable t) {
Log.e(TAG, "Error: " + t.getMessage());
}
});
}
RecyclerView.Adapter
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
SubNodes subNodes = subNodesList.get(position);
holder.ivNews.setText(subNodes.image.getSrc());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.tvText.setText(Html.fromHtml(subNodes.getBody(), Html.FROM_HTML_MODE_LEGACY));
} else {
holder.tvText.setText(Html.fromHtml(subNodes.getBody()));
}
}