【发布时间】:2017-12-09 11:23:12
【问题描述】:
在我的应用程序中,我想在recyclerView 中显示一些数据,然后我从服务器获取这些数据。
我将Retrofit 用于从 android 到服务器的 send 或 get 请求。
运行应用程序时,它显示 response.body().getData().size() 为 0,但在 PostMan 中我看到以下数据:
我写下面的代码来获取数据:
private void getComments() {
CommentSendData sendData = new CommentSendData();
sendData.setEntityID(7);
sendData.setReviewType(5);
sendData.setReviewUserType(0);
sendData.setEntityID(newsID);
sendData.setCelebrityID(0);
sendData.setPageIndex(0);
sendData.setPageSize(10);
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CommentResponse> call = api.getComments(sendData);
call.enqueue(new Callback<CommentResponse>() {
@Override
public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
Toast.makeText(NewsDetailActivity.this, "" + response.body().getData().size(), Toast.LENGTH_SHORT).show();
Toast.makeText(NewsDetailActivity.this, "" + call.isExecuted(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<CommentResponse> call, Throwable t) {
}
});
}
当显示此Toast :Toast.makeText(NewsDetailActivity.this, "" + call.isExecuted(), Toast.LENGTH_SHORT).show(); 时,在toast 中显示true。
当显示 Toast : Toast.makeText(NewsDetailActivity.this, "" + response.body().getData().size(), Toast.LENGTH_SHORT).show(); 时,在 toast 中显示 0。
我的适配器代码:
public class CommentsListAdapter extends RecyclerView.Adapter<CommentsListAdapter.ViewHolder> {
private Context context;
private List<CommentData> model;
public CommentsListAdapter(Context context, List<CommentData> model) {
this.context = context;
this.model = model;
}
@Override
public CommentsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_comment, parent, false);
return new CommentsListAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final CommentsListAdapter.ViewHolder holder, final int position) {
holder.row_commentNameTxt.setText(Html.fromHtml(model.get(position).getOwner().getName()));
holder.row_commentCommentTxt.setText(Html.fromHtml(model.get(position).getText()));
Glide.with(context)
.load(model.get(position).getOwner().getImageUrl())
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(holder.row_commentProfileImage);
holder.row_commentLikeTxt.setText(model.get(position).getLikeCount() + "");
holder.row_commentReplayTxt.setText(model.get(position).getRepliesCount() + "");
holder.row_commentDateTxt.setText(model.get(position).getSubmitDate() + " " + model.get(position).getSubmitTime());
}
@Override
public int getItemCount() {
return model.size();
}
public void addNewItem(List<CommentData> newContent) {
int start = this.model.size();
int end = newContent.size();
model.addAll(newContent);
notifyDataSetChanged();
}
public void clear() {
model.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView row_commentProfileImage;
private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentReplayTxt, row_commentDateTxt;
public ViewHolder(View itemView) {
super(itemView);
row_commentProfileImage = (CircleImageView) itemView.findViewById(R.id.row_commentProfileImage);
row_commentNameTxt = (TextView) itemView.findViewById(R.id.row_commentNameTxt);
row_commentCommentTxt = (TextView) itemView.findViewById(R.id.row_commentCommentTxt);
row_commentLikeTxt = (TextView) itemView.findViewById(R.id.row_commentLikeTxt);
row_commentReplayTxt = (TextView) itemView.findViewById(R.id.row_commentReplayTxt);
row_commentDateTxt = (TextView) itemView.findViewById(R.id.row_commentDateTxt);
}
}
}
评论回复代码:
public class CommentResponse {
@SerializedName("statusCode")
@Expose
private Integer statusCode;
@SerializedName("statusMessage")
@Expose
private String statusMessage;
@SerializedName("data")
@Expose
private List<CommentData> data = null;
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
public List<CommentData> getData() {
return data;
}
public void setData(List<CommentData> data) {
this.data = data;
}
}
评论数据模型:
public class CommentData {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("text")
@Expose
private String text;
@SerializedName("likeCount")
@Expose
private Integer likeCount;
@SerializedName("repliesCount")
@Expose
private Integer repliesCount;
@SerializedName("submitDate")
@Expose
private String submitDate;
@SerializedName("submitTime")
@Expose
private String submitTime;
@SerializedName("owner")
@Expose
private CommentOwner owner;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getLikeCount() {
return likeCount;
}
public void setLikeCount(Integer likeCount) {
this.likeCount = likeCount;
}
public Integer getRepliesCount() {
return repliesCount;
}
public void setRepliesCount(Integer repliesCount) {
this.repliesCount = repliesCount;
}
public String getSubmitDate() {
return submitDate;
}
public void setSubmitDate(String submitDate) {
this.submitDate = submitDate;
}
public String getSubmitTime() {
return submitTime;
}
public void setSubmitTime(String submitTime) {
this.submitTime = submitTime;
}
public CommentOwner getOwner() {
return owner;
}
public void setOwner(CommentOwner owner) {
this.owner = owner;
}
}
为什么在运行应用程序时显示 0 数据但在 PostMan 中显示数据?!
【问题讨论】:
-
"运行应用程序时,它显示 response.body().getData().size() 为 0" -- 检查
code()、message()和errorBody(),可能您收到 HTTP 错误。 -
@CommonsWare,你在这里吗,我的朋友?
标签: android list arraylist android-recyclerview