Android 改造 - GET Json 数组

Android Retrofit - GET Json Array

当我在我的 onClick 侦听器中调用 getRetrofitArray() 函数以获取 JSon 数组时,我发现我的应用程序崩溃了。

我的代码如下,请帮帮我:

  public void getRetrofitArray() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
    Call<BikeStation> call = service.getJsonArrayData();

    call.enqueue(new Callback<BikeStation>() {
        @Override
        public void onResponse(Call<BikeStation> call, Response<BikeStation> response) {
            Log.e("response ", response.body().toString());
            Log.e("number ", response.body().getNumber().toString());
            Log.e("address ", response.body().getAddress().toString());
            Log.e("name ", response.body().getName().toString());

            Log.e("banking ", response.body().getBanking().toString());
            Log.e("bonus ", response.body().getBonus().toString());
            Log.e("bike_stands ", response.body().getBike_stands().toString());
            Log.e("available_bike_stands ", response.body().getAvailable_bike_stands().toString());
            Log.e("available_bikes ", response.body().getAvailable_bikes().toString());
            Log.e("status ", response.body().getStatus().toString());
            Log.e("last_update ", response.body().getLast_update().toString());
        }

        @Override
        public void onFailure(Call<BikeStation> call, Throwable t) {

            Log.e("Error: ", t.toString());
        }
    });
}

我遵循了在线教程 (https://www.youtube.com/watch?time_continue=2&v=T-XFHVEdTUA&feature=emb_logo),但运气不佳。除了

,我无法在 logcat 上看到任何日志输出
 java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

我正在使用的 JSON 数组具有以下结构:

[
{
"number": 42,
"contract_name": "dublin",
"name": "SMITHFIELD NORTH",
"address": "Smithfield North",
"position": {
  "lat": 53.349562,
  "lng": -6.278198
},
"banking": true,
"bonus": false,
"bike_stands": 30,
"available_bike_stands": 5,
"available_bikes": 25,
"status": "OPEN",
"last_update": 1589297754000
},
....

我认为这个问题与 非常相似,但我看不出哪里错了。请原谅我,

感谢您的帮助,

来自 REST 的响应 API 是一个数组,但您将 BikeStation 作为预期的响应类型传递。尝试将其更改为 Call<List<BikeStation>>Callback<List<BikeStation>> 并更改 return 输入 getJsonArrayData().