预期 BEGIN_ARRAY 但使用 Retrofit 时 BEGIN_OBJECT
Expected BEGIN_ARRAY but was BEGIN_OBJECT with Retrofit
所以我正在尝试从日冕中加载数据 api。
这是我的界面:
public interface RKIApi {
String BASE_URL = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/";
@Headers("Content-Type: application/json")
@GET("query?where=1%3D1&outFields=cases,deaths,cases_per_population,county,death_rate&returnGeometry=false&outSR=4326&f=json")
Call<List<County>> getCounties();
}
这是我的县数据class,我希望将接收到的数据转换为:
public class County {
@SerializedName("county")
@Expose
private String county;
@SerializedName("cases")
@Expose
private int cases;
@SerializedName("deaths")
@Expose
private int deaths;
@SerializedName("cases_per_population")
@Expose
private float casesPerPopulation;
@SerializedName("death_rate")
@Expose
private float deathRate;
public County(String county, int cases, int deaths, float casesPerPopulation, float deathRate) {
this.county = county;
this.cases = cases;
this.deaths = deaths;
this.casesPerPopulation = casesPerPopulation;
this.deathRate = deathRate;
}
...吸气剂和吸气剂....
这就是我尝试加载数据的方式:
Retrofit retrofit = new Retrofit.Builder().baseUrl(RKIApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
RKIApi apiService = retrofit.create(RKIApi.class);
Call<List<County>> call = apiService.getCounties();
call.enqueue(new Callback<List<County>>() {
@Override
public void onResponse(@NonNull Call<List<County>> call,@NonNull Response<List<County>> response) {
... Do something ...
}
@Override
public void onFailure(@NonNull Call<List<County>> call,@NonNull Throwable t) {
System.out.println("========== ERROR ==========");
System.out.println(t.getMessage());
}
});
然而,当我尝试在启用调试器的情况下打开应用程序时,我得到的结果是 Expected BEGIN_ARRAY 但 BEGIN_OBJECT 在第 1 行第 2 列路径 $.现在这显然是因为 JSON 不是以县列表开头的,但是我只想加载它们。我已尝试使用上面看到的 Expose 标签,但它仍然无法正常工作。
非常感谢任何帮助:)
错误的问题和原因是设计不当 API 或错误解释 api。您的 api 没有发送对象列表。
很可能 API 吐出的 JSON 是这样的:
{ ...content }
这不是对象列表,而是单个对象。
Retrofit 期待这样的事情:
[ { ....County } { ..County }? ]
因为您告诉我们改造 return List<County>
的列表。
其中 [
表示对象数组。
所以为了解决这个问题,你有两个选择。要么更改 api,这样即使对于像这样 []
或 [{ one object }]
这样的空列表,它也会 return 始终是方括号。
要么手动解析您的结果。请在 Whosebug 上检查这个问题:
这意味着您的任何参数为空或 json 中没有任何内容。假设您解析 "name"= "pavel" 但在 json 中是 "name"=null
所以我正在尝试从日冕中加载数据 api。
这是我的界面:
public interface RKIApi {
String BASE_URL = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/";
@Headers("Content-Type: application/json")
@GET("query?where=1%3D1&outFields=cases,deaths,cases_per_population,county,death_rate&returnGeometry=false&outSR=4326&f=json")
Call<List<County>> getCounties();
}
这是我的县数据class,我希望将接收到的数据转换为:
public class County {
@SerializedName("county")
@Expose
private String county;
@SerializedName("cases")
@Expose
private int cases;
@SerializedName("deaths")
@Expose
private int deaths;
@SerializedName("cases_per_population")
@Expose
private float casesPerPopulation;
@SerializedName("death_rate")
@Expose
private float deathRate;
public County(String county, int cases, int deaths, float casesPerPopulation, float deathRate) {
this.county = county;
this.cases = cases;
this.deaths = deaths;
this.casesPerPopulation = casesPerPopulation;
this.deathRate = deathRate;
}
...吸气剂和吸气剂....
这就是我尝试加载数据的方式:
Retrofit retrofit = new Retrofit.Builder().baseUrl(RKIApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
RKIApi apiService = retrofit.create(RKIApi.class);
Call<List<County>> call = apiService.getCounties();
call.enqueue(new Callback<List<County>>() {
@Override
public void onResponse(@NonNull Call<List<County>> call,@NonNull Response<List<County>> response) {
... Do something ...
}
@Override
public void onFailure(@NonNull Call<List<County>> call,@NonNull Throwable t) {
System.out.println("========== ERROR ==========");
System.out.println(t.getMessage());
}
});
然而,当我尝试在启用调试器的情况下打开应用程序时,我得到的结果是 Expected BEGIN_ARRAY 但 BEGIN_OBJECT 在第 1 行第 2 列路径 $.现在这显然是因为 JSON 不是以县列表开头的,但是我只想加载它们。我已尝试使用上面看到的 Expose 标签,但它仍然无法正常工作。
非常感谢任何帮助:)
错误的问题和原因是设计不当 API 或错误解释 api。您的 api 没有发送对象列表。 很可能 API 吐出的 JSON 是这样的:
{ ...content }
这不是对象列表,而是单个对象。 Retrofit 期待这样的事情:
[ { ....County } { ..County }? ]
因为您告诉我们改造 return List<County>
的列表。
其中 [
表示对象数组。
所以为了解决这个问题,你有两个选择。要么更改 api,这样即使对于像这样 []
或 [{ one object }]
这样的空列表,它也会 return 始终是方括号。
要么手动解析您的结果。请在 Whosebug 上检查这个问题:
这意味着您的任何参数为空或 json 中没有任何内容。假设您解析 "name"= "pavel" 但在 json 中是 "name"=null