【问题标题】:Retrofit Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ [duplicate]改造错误:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ [重复] 处为 BEGIN_ARRAY
【发布时间】:2017-02-14 06:24:54
【问题描述】:

学习如何在我的 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;
    }

}

【问题讨论】:

  • 不知道哪里做错了..
  • Retrofit 无法将传入的 json 映射到您的 Lottery 类。发布 json,你会得到一些帮助。
  • 这里是 JSON 数据的链接,data.ny.gov/resource/…
  • [{"draw_date":"2016-10-04T00:00:00.000","mega_ball":"01","multiplier":"05","winning_numbers":"18 29 30 54 66"} ,{"draw_date":"2016-09-30T00:00:00.000","mega_ball":"09","multiplier":"05","winning_numbers":"21 30 47 50 57"} ,{"draw_date":"2016-09-27T00:00:00.000","mega_ball":"04","multiplier":"04","winning_numbers":"14 16 26 53 72"} ,{"draw_date ":"2016-09-23T00:00:00.000","mega_ball":"14","multiplier":"03","winning_numbers":"01 05 08 25 62"} ]
  • 这是 JSON DATA Ivan 的示例,再次感谢,任何帮助都会很棒。

标签: java android gson retrofit


【解决方案1】:

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

尝试将Call&lt;Lottery&gt; getLottery(); 更改为Call&lt;List&lt;Lottery&gt;&gt; getLottery();。您还需要在 Retrofit 调用定义中对其进行更改。

【讨论】:

  • 现在去试一试,一旦完成,伊万会提供更新
  • Ivan,你的意思是我需要在我的呼叫定义中更改它?你能告诉我在改造调用定义中需要改变什么吗,谢谢
  • Ivan 我在 Retrofit 调用中也改了错误
  • @Override public void onResponse(Call> call, Response response) { Log.d(TAG, "getting Draw Date"); Log.d(TAG, "绘制日期为:" + response.body().getDrawDate()); String DRAW_DATE = response.body().getDrawDate(); drawDate.setText("DRAW_DATE"); Log.d(TAG, "完成设置绘制日期"); }
  • Ivan,你能看看我把代码上传到了github
【解决方案2】:

你应该改变 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, "绘制日期为:" + 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());
        }
    });
}

【讨论】:

  • Tsiro,我进行了更改,在 LotteryObj 上出现错误,知道从哪里调用,
  • 你得到什么错误?
  • 我可以把我的完整项目发给你吗,或者某种 webex..lol
  • 当我复制我的 pojo 并将其替换为您创建的 pojo 时,收到 lotteryObj 错误,就像它不知道一样
  • 如果您有帐户,请在 Github 上上传您的项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多