当前位置: 首页 > 知识库问答 >
问题:

改型错误:java。lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径$[duplicate]处为BEGIN_数组

孔运良
2023-03-14

正在学习如何在我的Android应用程序中使用改型,出现以下错误:如果可以,请提供帮助。非常感谢。

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;
        }
    }
}

以下是我的建议:

@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个答案

樊烨烨
2023-03-14

您应该更改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();

然后在onSolutions()回调中,您应该遍历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");

//日志。d(标签,“绘制日期为:”response.body()。getDrawDate());//字符串DRAW_DATE=响应。body()。getDrawDate();提款日期。setText(“提款日期”);日志d(标记“完成绘制日期设置”);}

        @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());
        }
    });
}
东明德
2023-03-14

问题是,endpoint正在返回彩票类项目的列表,而您正在请求改装以将响应映射到单个彩票项目。

尝试更改呼叫

 类似资料: