【发布时间】:2020-05-05 06:31:24
【问题描述】:
所以我正在尝试从 corona 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();
}
这是我希望将收到的数据转换为的县数据类:
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;
}
... Getter 和 Setter....
这就是我尝试加载数据的方式:
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());
}
});
但是,当我尝试在启用调试器的情况下打开应用程序时,我得到的只是预期的 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT。现在这显然是因为 JSON 不以县列表开头,但我只想加载它们。我已尝试使用如上所示的 Expose Tag,但它仍然无法正常工作。
非常感谢任何帮助:)
【问题讨论】: