Android正在破坏我的字符串

时间:2016-06-23 13:32:01

标签: android android-intent

我有一个Post对象,我将其传递给intent:

Log.d("POST_IMAGE", post.image_url); http://localhost:8888/wordpress/wp-content/uploads/2016/06/yudikcflq9lsulgtwncn.jpg

final Intent intent = new Intent(); intent.setClass(host, PostDetail.class); intent.putExtra(PostDetail.EXTRA_POST, (Post) getItem(holder.getAdapterPosition())); host.startActivity(intent);

我的PostDetail活动中的OnCreate方法:

post = getIntent().getParcelableExtra(EXTRA_POST); Log.d("POST_IMAGE", post.image_url); ��������������S��http://localhost:8888/wordpress/wp-content/uploads/

检索到我的image_url后,我得到了这个奇怪的字符串,而不是��������������S��http://localhost:8888/wordpress/wp-content/uploads/

在我制作意图之前,图像字符串显示正常,但在传递之后,我得到一个格式错误的字符串。我的对象上的其他属性看起来很好。它只是这个变量。

编辑:我的帖子课

`package my.package.model;

    import android.content.res.ColorStateList;
    import android.os.Parcel;
    import android.os.Parcelable;
    import android.support.annotation.ColorInt;
    import android.text.TextUtils;

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

public class Post实现了Parcelable {

public final String comment;
public final String comment_html;
public final int comment_count;
public final long views_count;
public final int vote_count;
public final Date created_at;
public final long user_id;
public final String user_display_name;
public final String user_portrait_url;
public final String hostname;
public final String badge;
public final String user_job;
public final List<Comment> comments;
public final String image_url;

public Post(long id,
             String title,
             String url,
             String comment,
             String comment_html,
             int comment_count,
             long views_count,
             int vote_count,
             Date created_at,
             long user_id,
             String user_display_name,
             String user_portrait_url,
             String hostname,
             String badge,
             String user_job,
             List<Comment> comments,
             String image_url) {
    this.comment = comment;
    this.comment_html = comment_html;
    this.comment_count = comment_count;
    this.views_count = views_count;
    this.vote_count = vote_count;
    this.created_at = created_at;
    this.user_id = user_id;
    this.user_display_name = user_display_name;
    this.user_portrait_url = user_portrait_url;
    this.hostname = hostname;
    this.badge = badge;
    this.user_job = user_job;
    this.comments = comments;
    this.image_url = image_url;
}

protected Post(Parcel in) {
    super(in.readLong(), in.readString(), in.readString());
    comment = in.readString();
    comment_html = in.readString();
    comment_count = in.readInt();
    views_count = in.readLong();
    vote_count = in.readInt();
    long tmpCreated_at = in.readLong();
    created_at = tmpCreated_at != -1 ? new Date(tmpCreated_at) : null;
    user_id = in.readLong();
    user_display_name = in.readString();
    user_portrait_url = in.readString();
    hostname = in.readString();
    badge = in.readString();
    user_job = in.readString();
    if (in.readByte() == 0x01) {
        comments = new ArrayList<Comment>();
        in.readList(comments, Comment.class.getClassLoader());
    } else {
        comments = null;
    }
    image_url = in.readString();
}

public static class Builder {
    private long id;
    private String title;
    private String url;
    private String comment;
    private String commentHtml;
    private int commentCount;
    private long viewCount;
    private int voteCount;
    private Date createdAt;
    private long userId;
    private String userDisplayName;
    private String userPortraitUrl;
    private String hostname;
    private String badge;
    private String userJob;
    private List<Comment> comments;
    private String imageUrl;

    public Builder setId(long id) {
        this.id = id;
        return this;
    }

    public Builder setTitle(String title) {
        this.title = title;
        return this;
    }

    public Builder setUrl(String url) {
        this.url = url;
        return this;
    }

    public Builder setDefaultUrl(long id) {
        this.url = "https://www.site.co/posts/" + id;
        return this;
    }

    public Builder setComment(String comment) {
        this.comment = comment;
        return this;
    }

    public Builder setCommentHtml(String comment_html) {
        this.commentHtml = comment_html;
        return this;
    }

    public Builder setCommentCount(int comment_count) {
        this.commentCount = comment_count;
        return this;
    }

    public Builder setViewCount(long view_count) {
        this.viewCount = view_count;
        return this;
    }

    public Builder setVoteCount(int vote_count) {
        this.voteCount = vote_count;
        return this;
    }

    public Builder setCreatedAt(Date created_at) {
        this.createdAt = created_at;
        return this;
    }

    public Builder setUserId(long user_id) {
        this.userId = user_id;
        return this;
    }

    public Builder setUserDisplayName(String user_display_name) {
        this.userDisplayName = user_display_name;
        return this;
    }

    public Builder setUserPortraitUrl(String user_portrait_url) {
        this.userPortraitUrl = user_portrait_url;
        return this;
    }

    public Builder setHostname(String hostname) {
        this.hostname = hostname;
        return this;
    }

    public Builder setBadge(String badge) {
        this.badge = badge;
        return this;
    }

    public Builder setUserJob(String user_job) {
        this.userJob = user_job;
        return this;
    }

    public Builder setComments(List<Comment> comments) {
        this.comments = comments;
        return this;
    }

    public Builder setImageUrl(String image_url) {
        this.imageUrl = image_url;
        return  this;
    }

    public Post build() {
        return new Post(id, title, url, comment, commentHtml, commentCount, viewCount, voteCount,
                createdAt, userId, userDisplayName, userPortraitUrl, hostname, badge,
                userJob, comments, imageUrl);
    }

    public static Builder from(Post existing) {
        return new Builder()
                .setId(existing.id)
                .setTitle(existing.title)
                .setUrl(existing.url)
                .setComment(existing.comment)
                .setCommentHtml(existing.comment_html)
                .setCommentCount(existing.comment_count)
                .setViewCount(existing.views_count)
                .setVoteCount(existing.vote_count)
                .setCreatedAt(existing.created_at)
                .setUserId(existing.user_id)
                .setUserDisplayName(existing.user_display_name)
                .setUserPortraitUrl(existing.user_portrait_url)
                .setHostname(existing.hostname)
                .setBadge(existing.badge)
                .setUserJob(existing.user_job)
                .setComments(existing.comments)
                .setImageUrl(existing.image_url);
    }
}

/* Parcelable stuff */

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(id);
    dest.writeString(title);
    dest.writeString(url);
    dest.writeString(comment);
    dest.writeString(comment_html);
    dest.writeInt(comment_count);
    dest.writeInt(vote_count);
    dest.writeLong(created_at != null ? created_at.getTime() : -1L);
    dest.writeLong(user_id);
    dest.writeString(user_display_name);
    dest.writeString(user_portrait_url);
    dest.writeString(hostname);
    dest.writeString(badge);
    dest.writeString(user_job);
    if (comments == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(comments);
    }
    dest.writeString(image_url);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Post> CREATOR = new Parcelable.Creator<Post>() {
    @Override
    public Post createFromParcel(Parcel in) {
        return new Post(in);
    }

    @Override
    public Post[] newArray(int size) {
        return new Post[size];
    }
};

}`

1 个答案:

答案 0 :(得分:0)

我认为这是你的问题。你在writeToParcel()

中有这个
if (comments == null) {
    dest.writeByte((byte) (0x00));
} else {
    dest.writeByte((byte) (0x01));
    dest.writeList(comments);
}
dest.writeString(image_url);

如果他们在那里,会添加评论列表,但如果没有,则跳过该列表。在创建者中,您总是加载列表。

.setBadge(existing.badge)
.setUserJob(existing.user_job)
.setComments(existing.comments)
.setImageUrl(existing.image_url);

你有没有想过看看这是否只发生在有评论的帖子上?