【发布时间】:2016-09-30 05:35:15
【问题描述】:
我想从服务器加载数据并显示到recyclerView,对于连接我使用Retrofit2。
首先我加载categoryData,没关系,我可以看到categoryData。但是当点击类别帖子时,我想将此帖子数据加载到其他活动中,但是当转到此 activity 不显示数据!
我通过 postID 来加载此帖子并将此 postID 设置为 URL。
使用此代码,我使用此代码将 postID 从 categoryAdapter 传递到 activity:
((DataViewHolder) holder).main_post_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getPosition();
R_CatModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), PostShow_page.class)
.putExtra("postID", model.getId())
postShowActivity retrofit ApiInterface :
@GET("api/get_post")
Call<R_PostModelResponse> getPostResponse(@Query("post_id") Integer id);
postShowActivity 代码:
private void bindData() {
// Setup Connect
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<R_PostModelResponse> call = apiInterface.getPostResponse(postID);
call.enqueue(new Callback<R_PostModelResponse>() {
@Override
public void onResponse(Call<R_PostModelResponse> call, Response<R_PostModelResponse> response) {
if (response != null) {
models = response.body().getPost();
mAdaper = new CommentAdapter2(context, models);
Toast.makeText(PostShow_page.this, "GoTo Adapter", Toast.LENGTH_SHORT).show();
comment_Recyclerview.setAdapter(mAdaper);
}
}
@Override
public void onFailure(Call<R_PostModelResponse> call, Throwable t) {
}
});
}
postShowAdapter 代码:
public class CommentAdapter2 extends RecyclerView.Adapter<CommentAdapter2.ViewHolder> {
private List<R_PostModel> mDataSet;
private Context context;
public CommentAdapter2(Context context,List<R_PostModel> mDataSet) {
this.mDataSet = mDataSet;
this.context = context;
}
@Override
public CommentAdapter2.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(CommentAdapter2.ViewHolder viewHolder, int position) {
viewHolder.comment_name.setText(mDataSet.get(position).getComments().get(1).getCmName());
viewHolder.comment_date.setText(mDataSet.get(position).getComments().get(4).getCmDate());
viewHolder.comment_content.setText(mDataSet.get(position).getComments().get(3).getCmContent());
}
@Override
public int getItemCount() {
return mDataSet.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView comment_name, comment_content, comment_date;
public ViewHolder(View view) {
super(view);
comment_name = (TextView) itemView.findViewById(R.id.comment_userName_text);
comment_content = (TextView) itemView.findViewById(R.id.comment_comment_text);
comment_date = (TextView) itemView.findViewById(R.id.comment_date_text);
}
}
}
postDataModel:
public class R_PostModel {
@SerializedName("id")
public Integer id;
@SerializedName("type")
public String type;
@SerializedName("slug")
public String slug;
@SerializedName("url")
public String url;
@SerializedName("status")
public String status;
@SerializedName("title")
public String title;
@SerializedName("title_plain")
public String title_plain;
@SerializedName("content")
public String content;
@SerializedName("excerpt")
public String excerpt;
@SerializedName("date")
public String date;
@SerializedName("modified")
public String modified;
@SerializedName("comment_count")
public int comment_count;
@SerializedName("comment_status")
public String comment_status;
@SerializedName("comments")
public List<R_PostComment> comments;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle_plain() {
return title_plain;
}
public void setTitle_plain(String title_plain) {
this.title_plain = title_plain;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public int getComment_count() {
return comment_count;
}
public void setComment_count(int comment_count) {
this.comment_count = comment_count;
}
public String getComment_status() {
return comment_status;
}
public void setComment_status(String comment_status) {
this.comment_status = comment_status;
}
public List<R_PostComment> getComments() {
return comments;
}
public void setComments(List<R_PostComment> comments) {
this.comments = comments;
}
postShowModelResponse:
public class R_PostModelResponse {
@SerializedName("status")
public String status;
@SerializedName("post")
public List<R_PostModel> post;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<R_PostModel> getPost() {
return post;
}
public void setPost(List<R_PostModel> post) {
this.post = post;
}
}
相同的 json:
{
"status": "ok",
"post": {
"id": 2289,
"type": "post",
"slug": "11",
"url": "http:\/\/site.com\/1393\/02\/12\/11\/",
"status": "publish",
"title": "Donec tempus urna risus",
"title_plain": "Donec tempus urna risus",
"content": "<p class=\"big\">Lorem ipsum dolor sit amet neque vitae mauris. Etiam malesuada ultricies. Nullam ut nunc odio eget volutpat a, rutrum ac, magna. Nulla facilisi. Nullam justo. Sed leo tristique senectus et ultrices sit amet, consectetuer adipiscing ornare. Nullam vulputate luctus. Nulla interdum libero. Maecenas tincidunt. Pellentesque dolor. In urna. Suspendisse sollicitudin. Vestibulum tempus purus fermentum imperdiet tincidunt, risus pede, luctus laoreet. Aenean ac eros quis eleifend congue. Nam dolor eget velit. Suspendisse at<\/p>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"35\">35<\/span><\/div><h3 class=\"title\">countries<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"142\">142<\/span><\/div><h3 class=\"title\">articles<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"column one-third\"><div class=\"quick_fact animate-math\"><div class=\"number-wrapper\"><span class=\"number\" data-to=\"89\">89<\/span><\/div><h3 class=\"title\">projects<\/h3><hr class=\"hr_narrow\" \/><div class=\"desc\">Donec vestibulum justo a diam ultricies pel lentesque. Quisque mattis diam vel lac.<\/div><\/div>\n<\/div>\n<div class=\"hr_dots\" style=\"margin: 0 auto 30px;\"><span><\/span><span><\/span><span><\/span><\/div>\n\n<p>Praesent odio ac turpis luctus eu, ornare varius, leo. Suspendisse sed metus. Class aptent taciti sociosqu ad litora torquent per inceptos hymenaeos. Sed sed est. Sed venenatis. Morbi tincidunt. Nullam justo. Vestibulum ut justo a odio. Etiam et pede eget metus nonummy at, rhoncus dolor in interdum pellentesque quis, lacinia aliquet. In mauris sit amet leo. Aliquam erat consectetuer vestibulum varius. Cras ut orci at lorem odio tellus hendrerit sed, congue fringilla.<\/p>\n<p>Class aptent taciti sociosqu ad litora torquent per inceptos hymenaeos. Maecenas vehicula, dui nulla, egestas sodales, augue commodo nec, ullamcorper ligula lorem odio sit amet, vestibulum wisi ultricies eu, magna. Fusce aliquet elit, gravida tellus tristique eget, porta eget, eros. In hac habitasse platea dictumst. Proin cursus magna. Sed placerat. Mauris vel risus. Nunc accumsan dictum, laoreet viverra. Cras tempus purus at risus auctor tincidunt. Sed sed tortor. In venenatis consequat. Donec iaculis. Curabitur est pretium wisi, sed leo. Sed tincidunt risus neque ultrices consectetuer. Etiam mollis ut, dolor. Maecenas.<\/p>\n",
"excerpt": "<p>Donec tempus, urna risus nec mauris. Lorem ipsum primis in nulla ac arcu vitae augue. Vivamus nec elit a dui. Morbi id leo nec dui. Maecenas.<\/p>\n",
"date": "\u06f1\u06f3\u06f9\u06f3-\u06f0\u06f2-\u06f1\u06f2 \u06f0\u06f9:\u06f3\u06f8:\u06f3\u06f1",
"modified": "2014-05-02 09:38:31",
"comments": [{
"id": 10,
"name": "محمد",
"url": "",
"date": "2016-09-28 00:00:00",
"content": "<p>تست پيام از سمت برنامه نويسي<\/p>\n",
"parent": 0
}, {
"id": 11,
"name": "Mohammad1",
"url": "",
"date": "2016-09-28 00:00:00",
"content": "<p>Test Comment 1<\/p>\n",
"parent": 0
}, {
为了测试,我在 postShowActivity retrofit 响应中插入 Toast 消息,但 不显示此 Toast!
Toast.makeText(PostShow_page.this, "GoTo Adapter", Toast.LENGTH_SHORT).show();
更新: 运行应用时,不调用onResponse,调用onFailure!
如何解决这个问题并显示数据?
【问题讨论】:
-
尝试调试看看是调用onResponse还是onFailure
-
@Manifest,我测试一下,调用onFailure
-
@Manifest,我的朋友,你能帮帮我吗?我真的需要这个
-
@Manifest,谢谢兄弟,我等着
-
@Kmh54:检查您从改造中得到的响应,可能是 json 解析中的一些问题,这就是它无法在 R_PostModelResponse 中解析的原因。
标签: android retrofit2 android-json gson