改造无数据
Retrofit no data
我正在尝试从这个 api 获取数据,并在 android studio 中进行改造。
https://services5.arcgis.com/fsYDFeRKu1hELJJs/arcgis/rest/services/FOHM_Covid_19_FME_1/FeatureServer/4/query?f=geojson&where=1%3D1&outFields=%C3%85ldersgrupp&outFields=Totalt_antal_fall&outFields=Totalt_antal_avlidna
我得到的数据要么是 null 要么是 0,我不明白为什么。
这是我的界面:
public interface apiCall {
@GET("4/query?f=geojson&where=1%3D1&outFields=Åldersgrupp2&outFields=Totalt_antal_fall&outFields=Totalt_antal_avlidna&outFields=Totalt_antal_intensivvårdade")
Call<apiData> getData();
}
这是我的模型
@SerializedName("Totalt_antal_avlidna")
private int death;
@SerializedName("Totalt_antal_fall")
private int cases;
@SerializedName("Totalt_antal_intensivvårdade")
private int hospital;
@SerializedName("Åldersgrupp")
private String ageGroup;
public int getDeath() {
return death;
}
public int getCases() {
return cases;
}
public int getHospital() {
return hospital;
}
public String getAgeGroup() {
return ageGroup;
}
这是代码:
//retrofit builder
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://services5.arcgis.com/fsYDFeRKu1hELJJs/arcgis/rest/services/FOHM_Covid_19_FME_1/FeatureServer/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//interface
apiCall callApi = retrofit.create(apiCall.class);
Call<apiData> call = callApi.getData();
call.enqueue(new Callback<apiData>() {
@Override
public void onResponse(Call<apiData> call, Response<apiData> response) {
if(!response.isSuccessful()){
test.setText("code " + response.code());
return;
}
apiData trying = response.body();
String content = "";
content += "age group: " + trying.getAgeGroup() +"\n";
content += "cases: " + trying.getCases() +"\n";
content += "death: " + trying.getDeath() +"\n";
content += "hospital: " + trying.getHospital() +"\n\n";
test.append(content);
}
@Override
public void onFailure(Call<apiData> call, Throwable t) {
test.setText(t.getMessage());
}
});
您需要解析“FeatureCollection”,然后是“Features”,然后是实际特征。
所以你类的结构不对
修复对象的层次结构,你就会解决它
//这只是下面的伪代码,但它应该为您指明正确的方向:
@SerializedName("FeatureCollection")
class FeatureCollection{
@SerializedName("Features")
ArrayList<Feature> features;
}
class Feature{
@SerializedName("Totalt_antal_intensivvårdade")
int hospital;
@SerializedName("Åldersgrupp")
String ageGroup;
}
我正在尝试从这个 api 获取数据,并在 android studio 中进行改造。 https://services5.arcgis.com/fsYDFeRKu1hELJJs/arcgis/rest/services/FOHM_Covid_19_FME_1/FeatureServer/4/query?f=geojson&where=1%3D1&outFields=%C3%85ldersgrupp&outFields=Totalt_antal_fall&outFields=Totalt_antal_avlidna
我得到的数据要么是 null 要么是 0,我不明白为什么。
这是我的界面:
public interface apiCall {
@GET("4/query?f=geojson&where=1%3D1&outFields=Åldersgrupp2&outFields=Totalt_antal_fall&outFields=Totalt_antal_avlidna&outFields=Totalt_antal_intensivvårdade")
Call<apiData> getData();
}
这是我的模型
@SerializedName("Totalt_antal_avlidna")
private int death;
@SerializedName("Totalt_antal_fall")
private int cases;
@SerializedName("Totalt_antal_intensivvårdade")
private int hospital;
@SerializedName("Åldersgrupp")
private String ageGroup;
public int getDeath() {
return death;
}
public int getCases() {
return cases;
}
public int getHospital() {
return hospital;
}
public String getAgeGroup() {
return ageGroup;
}
这是代码:
//retrofit builder
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://services5.arcgis.com/fsYDFeRKu1hELJJs/arcgis/rest/services/FOHM_Covid_19_FME_1/FeatureServer/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//interface
apiCall callApi = retrofit.create(apiCall.class);
Call<apiData> call = callApi.getData();
call.enqueue(new Callback<apiData>() {
@Override
public void onResponse(Call<apiData> call, Response<apiData> response) {
if(!response.isSuccessful()){
test.setText("code " + response.code());
return;
}
apiData trying = response.body();
String content = "";
content += "age group: " + trying.getAgeGroup() +"\n";
content += "cases: " + trying.getCases() +"\n";
content += "death: " + trying.getDeath() +"\n";
content += "hospital: " + trying.getHospital() +"\n\n";
test.append(content);
}
@Override
public void onFailure(Call<apiData> call, Throwable t) {
test.setText(t.getMessage());
}
});
您需要解析“FeatureCollection”,然后是“Features”,然后是实际特征。
所以你类的结构不对
修复对象的层次结构,你就会解决它
//这只是下面的伪代码,但它应该为您指明正确的方向:
@SerializedName("FeatureCollection")
class FeatureCollection{
@SerializedName("Features")
ArrayList<Feature> features;
}
class Feature{
@SerializedName("Totalt_antal_intensivvårdade")
int hospital;
@SerializedName("Åldersgrupp")
String ageGroup;
}