谷歌Gson导致stackoverflow

时间:2014-07-14 08:52:21

标签: android json gson

Error    
 07-14 04:33:28.030: E/AndroidRuntime(5216): FATAL EXCEPTION: main
    07-14 04:33:28.030: E/AndroidRuntime(5216): Process: com.tt, PID: 5216
    07-14 04:33:28.030: E/AndroidRuntime(5216): java.lang.StackOverflowError
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.String._getChars(String.java:908)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:216)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:154)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Game.toString(Game.java:157)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.util.AbstractCollection.toString(AbstractCollection.java:374)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.java:202)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at com.tt.models.Player.toString(Player.java:161)
    07-14 04:33:28.030: E/AndroidRuntime(5216):     at java.lang.StringBuilder.append(StringBuilder.ja

我的模特玩家

package com.tt.models;
import java.util.Collection;
import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "Player")
public class Player {
    public static final String BATTING_ID_FIELD_NAME = "batting_id";
    public static final String BOWLING_ID_FIELD_NAME = "bowling_id";
    public static final String TEAM_ID_FIELD_NAME = "team_id";
    @SerializedName("__v")
    @DatabaseField(id = true) 
     private int id;

    @SerializedName("_id")
    @DatabaseField(columnName = "_id")
    private String _id;
    /*@SerializedName("__v")
    @DatabaseField(columnName = "__v",id=true)
    private int __v;*/
    @DatabaseField
    private String battingStyle;
    @DatabaseField
    private String bowlingStyle;
    @DatabaseField
    private String dob;
    @DatabaseField
    private String playingRole;
    @DatabaseField
    private String sname;
    @DatabaseField
    private String fname;
    @SerializedName("gallery")
    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private Collection<Gallery> gallery;

    @SerializedName("games")
    @ForeignCollectionField(eager = true)
    private Collection<Game> games;

    /*
     * @DatabaseField(dataType = DataType.SERIALIZABLE) private String[] users;
     */
    /*
     * @DatabaseField(dataType = DataType.SERIALIZABLE) private String[] squad;
     */

    Player() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getBattingStyle() {
        return battingStyle;
    }

    public void setBattingStyle(String battingStyle) {
        this.battingStyle = battingStyle;
    }

    public String getBowlingStyle() {
        return bowlingStyle;
    }

    public void setBowlingStyle(String bowlingStyle) {
        this.bowlingStyle = bowlingStyle;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getPlayingRole() {
        return playingRole;
    }

    public void setPlayingRole(String playingRole) {
        this.playingRole = playingRole;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    /*
     * public String[] getUsers() { return users; }
     * 
     * public void setUsers(String[] users) { this.users = users; }
     */

    /*
     * public String[] getSquad() { return squad; }
     * 
     * public void setSquad(String[] squad) { this.squad = squad; }
     */

    public Collection<Gallery> getGallerys() {
        return gallery;
    }

    public void setGallerys(Collection<Gallery> gallery) {
        this.gallery = gallery;
    }

    public Collection<Gallery> getGallery() {
        return gallery;
    }

    public void setGallery(Collection<Gallery> gallery) {
        this.gallery = gallery;
    }

    public Collection<Game> getGames() {
        return games;
    }

    public void setGames(Collection<Game> games) {
        this.games = games;
    }

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    @Override
    public String toString() {
        return "Player [id=" + Integer.toString(id) + ", _id=" + _id + ", battingStyle="
                + battingStyle + ", bowlingStyle=" + bowlingStyle + ", dob="
                + dob + ", playingRole=" + playingRole + ", sname=" + sname
                + ", fname=" + fname + ", gallery=" + gallery + ", games="
                + games + "]";
    }






}
and game model 
package com.tt.models;

import java.util.Collection;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "Game")
public class Game {
    public static final String INNING_ID_FIELD_NAME = "inning_id";
    public static final String PLAYER_ID_FIELD_NAME = "player_id";
    @SerializedName("__v")
    @DatabaseField(id = true) 
     private int id;

    /*@SerializedName("_id")
    @DatabaseField(columnName = "_id")
    private String _id;
    @SerializedName("__v")
    @DatabaseField(columnName = "__v",id=true)
    private int __v;*/
    @SerializedName("gameType")
    @DatabaseField
    private String gameType;
    @SerializedName("name")
    @DatabaseField
    private String name;
    @SerializedName("totalOvers")
    @DatabaseField
    private String totalOvers;
    @SerializedName("dateStarted")
    @DatabaseField
    private String dateStarted;
    @SerializedName("dateEnded")
    @DatabaseField
    private String dateEnded;
    @SerializedName("innings")
    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private Collection<Inning> innings;
    @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true, columnName = PLAYER_ID_FIELD_NAME)
    private Player player;
    @SerializedName("grounds")
    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private Collection<Ground> grounds;
    @SerializedName("teams")
    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private Collection<Team> teams;

    public Game() {
        // all persisted classes must define a no-arg constructor with at least
        // package visibility
    }



    /*public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }*/

    public String getGameType() {
        return gameType;
    }

    public int getId() {
        return id;
    }



    public void setId(int id) {
        this.id = id;
    }



    public void setGameType(String gameType) {
        this.gameType = gameType;
    }

    public Collection<Ground> getGrounds() {
        return grounds;
    }

    public void setGrounds(Collection<Ground> grounds) {
        this.grounds = grounds;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDateStarted() {
        return dateStarted;
    }

    public void setDateStarted(String dateStarted) {
        this.dateStarted = dateStarted;
    }

    public String getDateEnded() {
        return dateEnded;
    }

    public void setDateEnded(String dateEnded) {
        this.dateEnded = dateEnded;
    }

    public String getTotalOvers() {
        return totalOvers;
    }

    public void setTotalOvers(String totalOvers) {
        this.totalOvers = totalOvers;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public Collection<Inning> getInnings() {
        return innings;
    }

    public void setInnings(Collection<Inning> innings) {
        this.innings = innings;
    }

    public Collection<Team> getTeams() {
        return teams;
    }

    public void setTeams(Collection<Team> teams) {
        this.teams = teams;
    }



    @Override
    public String toString() {
        return "Game [id=" + Integer.toString(id) + ", gameType=" + gameType + ", name=" + name
                + ", totalOvers=" + totalOvers + ", dateStarted=" + dateStarted
                + ", dateEnded=" + dateEnded + ", innings=" + innings
                + ", player=" + player + ", grounds=" + grounds + ", teams="
                + teams + "]";
    }

    /*public int get__v() {
        return __v;
    }

    public void set__v(int __v) {
        this.__v = __v;
    }*/

1 个答案:

答案 0 :(得分:4)

PlayerGameGamePlayerPlayerGameGamePlayer ... stackoverflow。

更改班级的结构。消除循环引用,或在transientPlayer引用上使用Game关键字将其从序列化中排除。

编辑:

如果您不想更改课程结构,例如:每个游戏都引用了播放器,但播放器没有引用其游戏。然后你可以做这样的事情:

  • 序列化播放器
  • 播放器将序列化其游戏集合
  • 游戏课应该有private transient Player player;字段, 避免StackOverflowError
  • 反序列化之后,播放器应该迭代其游戏和 每个人都致电game.setPlayer(this);

您还需要修复toString()方法。由于Player类的toString()方法调用GametoString()调用Player类&{39} toString()

当你StackOverflowError得到这样的事情时。或者不要像这样构建类结构,其中每个Player都有Games,每个Game都有Player