1

正在寻找答案,但找不到对我有帮助的答案。我正在使用 volley - json 请求我收到错误:java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

这是我的代码:

    public static JsonTask<GameStatusResponse> PostLiveGames(final Context ctx, ArrayList<GameStatus> liveGames)
{
    ArrayList<Integer> gamesIds = new ArrayList<Integer>();
    for (GameStatus gameStatus : liveGames)
    {
        gamesIds.add(gameStatus.ID);
    }

    JsonObject bodObj = new JsonObject();
    bodObj.addProperty("AcceptNotification", true);
    bodObj.addProperty("AppVersion", WZApp.getAppVersion(mContext));
    String ids = "";
    for (int i = 0; i < gamesIds.size(); i++)
    {
        ids = ids + "," + gamesIds.get(i);
    }

    ids = ids.substring(1, ids.length());

    bodObj.addProperty("games", "[" + ids + "]");
    System.out.println("live games object: " + bodObj.toString());

    JsonTask<GameStatusResponse> task = new JsonTask<GameStatusResponse>(getURL(APIMethods.GetLiveGames.url),
            GameStatusResponse.class, new JsonResponseCallback<GameStatusResponse>()
            {

                @Override
                public void onResponseReceived(GameStatusResponse response)
                {
                    if (response != null && response.response != null
                            && response.response.GameStatusGames.size() > 0)
                        System.out.println("check " + response.response.GameStatusGames.get(0).GameStatusType);
                }

                @Override
                public void onErrorReceived(Exception error)
                {
                    System.out.println("we got error in live games: " + error.getMessage());

                }
            });
    task.addCustomDeserializer(betStatusTypeDeserializer, DataEnums.BetStatusType.class)
            .addCustomDeserializer(betResultTypeDeserializer, BetResult.class)
            .addCustomDeserializer(gameStatusTypeDeserializer, DataEnums.WZGameStatusType.class)
            .addCustomDeserializer(jinxTypeDeserializer, DataEnums.JinxType.class)
            .addCustomDeserializer(betOddsOutcomesTypeDeserializer, DataEnums.WZOddsOut.class)
            .addCustomDeserializer(betOddsTypeDeserializer, DataEnums.WZOdds.class);

    task.setAuthHeader(buildAuthHeader(RequestType.Flow, mDataManager.getCurrUser())).setPostMethod(bodObj);

    if (WZApp.debugMode)
        task.setDebugMode(true);

    task.execute();

    return task;

这是我的响应类:

public class GameStatusResponse
{
public Meta meta;
public GameStatusObject response;

public class GameStatusObject
{
    public ArrayList<GameStatus> GameStatusGames;
}
 }

我的课:

    import java.util.ArrayList;
    import android.os.Parcel;
    import android.os.Parcelable;
    import com.j256.ormlite.field.DataType;
    import com.j256.ormlite.field.DatabaseField;
    import com.j256.ormlite.table.DatabaseTable;
    import com.moblin.wagerzone.DataEnums;
    @DatabaseTable
    public class GameStatus implements Parcelable
    {
@DatabaseField
public int ID;

@DatabaseField
public int T1_Result;

@DatabaseField
public int T2_Result;

@DatabaseField
public int TotalBets;

@DatabaseField
public boolean isEnded;

@DatabaseField
public int WinnerID;

@DatabaseField
public int GameResult;

@DatabaseField
public DataEnums.WZGameStatusType GameStatusType;

@DatabaseField(dataType = DataType.SERIALIZABLE, canBeNull = true)
public ArrayList<Odd> Odds;

public int priority;

public GameStatus()
{
}

public GameStatus(Parcel in)
{
    this.GameResult = in.readInt();
    this.WinnerID = in.readInt();
    this.TotalBets = in.readInt();
    this.ID = in.readInt();
    this.T1_Result = in.readInt();
    this.T2_Result = in.readInt();
    this.GameStatusType = DataEnums.WZGameStatusType.getById(in.readInt());
    boolean[] temp = new boolean[1];
    try
    {
        in.readBooleanArray(temp);
    } catch (Exception e)
    {
        // TODO: handle exception
    }

    isEnded = temp[0];
    if (Odds != null)
        in.readTypedList(Odds, Odd.CREATOR);
}

@Override
public int describeContents()
{
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
    dest.writeTypedList(Odds);
    dest.writeInt(this.WinnerID);
    dest.writeInt(this.GameResult);
    if (GameStatusType != null)
        dest.writeInt(this.GameStatusType.id);

    dest.writeInt(this.TotalBets);

    dest.writeInt(this.ID);

    dest.writeInt(this.T1_Result);
    dest.writeInt(this.T2_Result);
    dest.writeBooleanArray(new boolean[] { isEnded });

}

@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
    public GameStatus createFromParcel(Parcel in)
    {
        return new GameStatus(in);
    }

    public GameStatus[] newArray(int size)
    {
        return new GameStatus[size];
    }
};

public boolean isLive(GameStatus gameStatus)
{
    boolean retval = false;
    switch (gameStatus.GameStatusType)
    {
        case WZGameStatusCancelled:
            break;
        case WZGameStatusEnded:
            break;
        case WZGameStatusLive:
            retval = true;
            break;
        case WZGameStatusNew:
            break;
        case WZGameStatusWaiting:
            break;
        default:
            break;

    }
    return retval;
}

}

和日志猫:

08-20 10:28:04.485: I/System.out(26524): we got error in live games: Error parsing JSON: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 96

这里也是来自服务器的 json:

    response:
{
    meta =     {
        message = OK;
        "operation_message" = "<null>";
        operationstatus = 0;
        status = 200;
    };
    response =     (
                {
            GameResult = 3;
            GameStatusType = 3;
            ID = 106448;
            Odds =             (
                                {
                    OddID = 84001;
                    Outcomes =                     (
                                                {
                            ID = 4;
                            Name = 1;
                        },
                                                {
                            ID = 5;
                            Name = X;
                        },
                                                {
                            ID = 6;
                            Name = 2;
                        }
                    );
                    Result = "<null>";
                    TypeID = 10;
                    TypeName = ThreeWay;
                    Value = "<null>";
                }
            );
            "T1_Result" = 0;
            "T2_Result" = 0;
            TotalBets = 76;
            WinnerID = "<null>";
            isEnded = 0;
        },
                {
            GameResult = 3;
            GameStatusType = 3;
            ID = 107009;
            Odds =             (
                                {
                    OddID = 85377;
                    Outcomes =                     (
                                                {
                            ID = 7;
                            Name = 1;
                        },
                                                {
                            ID = 8;
                            Name = 2;
                        }
                    );
                    Result = "<null>";
                    TypeID = 20;
                    TypeName = TwoWay;
                    Value = "<null>";
                }
            );
            "T1_Result" = 0;
            "T2_Result" = 0;
            TotalBets = 0;
            WinnerID = "<null>";
            isEnded = 0;
        },
                {
            GameResult = 3;
            GameStatusType = 4;
            ID = 107087;
            Odds =             (
                                {
                    OddID = 85539;
                    Outcomes =                     (
                                                {
                            ID = 7;
                            Name = 1;
                        },
                                                {
                            ID = 8;
                            Name = 2;
                        }
                    );
                    Result = "<null>";
                    TypeID = 20;
                    TypeName = TwoWay;
                    Value = "<null>";
                }
            );
            "T1_Result" = 0;
            "T2_Result" = 0;
            TotalBets = 0;
            WinnerID = "<null>";
            isEnded = 0;
        }
    );
}

看不懂,求指教。

谢谢!

4

1 回答 1

0

你的输出看起来很奇怪。JSON结果是自己创造的吗?

response和字段似乎都Odd以开头( )而不是[ ]表示数组。最重要的是,这似乎根本不是有效的 JSON。

在 JSON 中,不使用=,但:和字段由;而不是. 分隔,。例如:

{
    meta : {
        message : OK,
        "operation_message" : "<null>",
        operationstatus : 0,
        status : 200,
    },
    response : [{
        GameResult : 3,
        GameStatusType : 3,
        ID : 106448,
        Odds : [{
            OddID : 84001,
            Outcomes : [{
                ID : 4,
                Name : 1
            }, {
                ID : 5,
                Name : X
            }, {
                ID : 6,
                Name : 2
            }],
            Result : "<null>",
            TypeID : 10,
            TypeName : ThreeWay,
            Value : "<null>"
        }],
        "T1_Result" : 0,
        "T2_Result" : 0,
        TotalBets : 76,
        WinnerID : "<null>",
        isEnded : 0
    }, {
    ....
于 2013-08-20T15:11:00.737 回答