改造错误:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2行路径$ BEGIN_ARRAY

时间:2016-10-05 17:24:20

标签: java android gson retrofit

了解如何在我的Android应用中使用Retrofit,收到以下错误: 如果可以的话请帮忙。谢谢,

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

这是我的代码:如果您的能力也是如此:

public void clickButton(View view){
    button = (Button) findViewById(R.id.button);
    drawDate = (TextView)findViewById(R.id.drawDate);

    LotteryAPI.Factory.getIstance().getLottery().enqueue(new Callback<Lottery>() {
        @Override
        public void onResponse(Call<Lottery> call, Response<Lottery> response) {
            Log.d(TAG, "getting Draw Date");
            Log.d(TAG, "Draw Date is: " + response.body().getDrawDate());
            String DRAW_DATE = response.body().getDrawDate();
            drawDate.setText("DRAW_DATE");
            Log.d(TAG, "done setting Draw Date");
        }

        @Override
        public void onFailure(Call<Lottery> call, Throwable t) {
            Log.e("Failed",  t.getMessage());
            Log.d(TAG, "At onFailure - Something Failed!!");
            Log.d(TAG, "error is: " + t.getCause());

        }
    });
}

这是我的界面:

String BASE_URL = "https://data.ny.gov/resource/h6w8-42p9.json/";

@GET("?$$app_token=xxxxxxGtxKw3s6gurSxxxxxx")
Call<Lottery> getLottery();


class Factory {
    public static LotteryAPI service;

    public static LotteryAPI getIstance() {
        if (service == null) {
            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(BASE_URL).build();
            service = retrofit.create(LotteryAPI.class);
            return service;
        } else {
            return service;
        }
    }
}

这是我的POJO:

@Generated("org.jsonschema2pojo")
public class Lottery {

    @SerializedName("draw_date")
    @Expose
    private String drawDate;
    @SerializedName("mega_ball")
    @Expose
    private String megaBall;
    @SerializedName("multiplier")
    @Expose
    private String multiplier;
    @SerializedName("winning_numbers")
    @Expose
    private String winningNumbers;

    /**
     * 
     * @return
     *     The drawDate
     */
    public String getDrawDate() {
        return drawDate;
    }

    /**
     * 
     * @param drawDate
     *     The draw_date
     */
    public void setDrawDate(String drawDate) {
        this.drawDate = drawDate;
    }

    /**
     * 
     * @return
     *     The megaBall
     */
    public String getMegaBall() {
        return megaBall;
    }

    /**
     * 
     * @param megaBall
     *     The mega_ball
     */
    public void setMegaBall(String megaBall) {
        this.megaBall = megaBall;
    }

    /**
     * 
     * @return
     *     The multiplier
     */
    public String getMultiplier() {
        return multiplier;
    }

    /**
     * 
     * @param multiplier
     *     The multiplier
     */
    public void setMultiplier(String multiplier) {
        this.multiplier = multiplier;
    }

    /**
     * 
     * @return
     *     The winningNumbers
     */
    public String getWinningNumbers() {
        return winningNumbers;
    }

    /**
     * 
     * @param winningNumbers
     *     The winning_numbers
     */
    public void setWinningNumbers(String winningNumbers) {
        this.winningNumbers = winningNumbers;
    }

}

2 个答案:

答案 0 :(得分:2)

问题是,端点正在返回您的彩票类项目列表,并且您要求Retrofit将响应映射到单个彩票项目。

尝试将Call<Lottery> getLottery();更改为Call<List<Lottery>> getLottery();。您还需要在Retrofit调用定义中更改它。

答案 1 :(得分:0)

你应该改变POJO类结构的结构,因为我可以看到你的json字符串是一个包含对象的数组。你必须到POJO课程:

class Lottery {
  private String draw_date, mega_ball, multiplier, winning_numbers;

public Lottery() {}

public String getDraw_date() {
    return draw_date;
}

public void setDraw_date(String draw_date) {
    this.draw_date = draw_date;
}

public String getWinning_numbers() {
    return winning_numbers;
}

public void setWinning_numbers(String winning_numbers) {
    this.winning_numbers = winning_numbers;
}

public String getMega_ball() {
    return mega_ball;
}

public void setMega_ball(String mega_ball) {
    this.mega_ball = mega_ball;
}

public String getMultiplier() {
    return multiplier;
}

public void setMultiplier(String multiplier) {
    this.multiplier = multiplier;
}

}

您还必须从

更改以下行到LotteryAPI界面
Call<Lottery> getLottery();

Call<List<Lottery>> getLottery();

然后在onResponse()回调中你应该迭代LotteryObj列表

 public void clickButton(View view){
    button = (Button) findViewById(R.id.button);
    drawDate = (TextView)findViewById(R.id.drawDate);

    LotteryAPI.Factory.getIstance().getLottery().enqueue(new Callback<List<Lottery>>() {
        @Override
        public void onResponse(Response<List<Lottery>> response, Retrofit retrofit) {
            Log.e(TAG, response.body()+"");
            for (Lottery lt : response.body()) {
                Log.e(TAG, lt.getDraw_date());
                Log.e(TAG, lt.getMega_ball());
                if (lt.getMultiplier() != null) Log.e(TAG, lt.getMultiplier());
                Log.e(TAG, lt.getWinning_numbers());
            }
                Log.d(TAG, "getting Draw Date");

// Log.d(TAG,“Draw Date is:”+ response.body()。getDrawDate()); // String DRAW_DATE = response.body()。getDrawDate();                   drawDate.setText( “DRAW_DATE”);                   Log.d(TAG,“完成设置绘制日期”);             }

        @Override
        public void onFailure(Throwable t) {
            Log.e("Failed",  t.getMessage());
            Log.d(TAG, "At onFailure - Something Failed!!");
            Log.d(TAG, "error is: " + t.getCause());
        }
    });
}