Search code examples
javaandroidapiretrofit

Retrofit no data


Im trying to get the data from this api with retrofit in 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

The data i get is either null or 0 and I don't understand why.

This is my interface:

 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();
}

This is my model

@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;
}

This is the code:

 //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());
        }
    });

Solution

  • You need to parse "FeatureCollection", then "Features", then the actual feature.

    So the structure of your classes is not right

    Fix the hierarchy of objects and you'll solve it

    //this is just pseudo code below but it should point you in the right direction:

    @SerializedName("FeatureCollection")
    class FeatureCollection{
    
        @SerializedName("Features")
        ArrayList<Feature> features;
    }
    
    
    class Feature{    
        @SerializedName("Totalt_antal_intensivvårdade")
        int hospital;
    
        @SerializedName("Åldersgrupp")
        String  ageGroup; 
    }