0

I've to download a Json file with a fields like this:

enter image description here

and so I've made this code:

    final Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(AirportService.SERVICE_ENDPOINT).build();


    AirportService service = retrofit.create(AirportService.class);

    service.getAirport()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<List<Airport>>()
            {
                List<Airport>airps = new ArrayList<Airport>();
                @Override
                public void onCompleted()
                {

                }

                @Override
                public void onError(Throwable e)
                {
                    e.printStackTrace();
                }

                @Override
                public void onNext(List<Airport> airports)
                {
                for(Airport air : airports)              
                {                 
                 Log.d("latitude",String.valueOf(air.getLatitudeDeg());
                 //Log.d("iso_c",air.getIsoCountry());
                }
            });

but the problem is that some fiels are null /0.0: in this case the log is latitude_deg = 0.0 and iso_country = null. This is the Airport class:

public class Airport {

private double latitudeDeg;
private double longitudeDeg;
private int elevationFt;
private String continent;
private String isoCountry;

/**
 *
 * @return
 * The latitudeDeg
 */
public double getLatitudeDeg() {
    return latitudeDeg;
}

/**
 *
 * @param latitudeDeg
 * The latitude_deg
 */
public void setLatitudeDeg(double latitudeDeg) {
    this.latitudeDeg = latitudeDeg;
}

/**
 *
 * @return
 * The longitudeDeg
 */
public double getLongitudeDeg() {
    return longitudeDeg;
}

/**
 *
 * @param longitudeDeg
 * The longitude_deg
 */
public void setLongitudeDeg(double longitudeDeg) {
    this.longitudeDeg = longitudeDeg;
}

/**
 *
 * @return
 * The elevationFt
 */
public int getElevationFt() {
    return elevationFt;
}

/**
 *
 * @param elevationFt
 * The elevation_ft
 */
public void setElevationFt(int elevationFt) {
    this.elevationFt = elevationFt;
}

/**
 *
 * @return
 * The continent
 */
public String getContinent() {
    return continent;
}

/**
 *
 * @param continent
 * The continent
 */
public void setContinent(String continent) {
    this.continent = continent;
}

/**
 *
 * @return
 * The isoCountry
 */
public String getIsoCountry() {
    return isoCountry;
}

/**
 *
 * @param isoCountry
 * The iso_country
 */
public void setIsoCountry(String isoCountry) {
    this.isoCountry = isoCountry;
}

}

that I've generated with JsonToPOJO schema.

What is the error that retrieve me the 0 / null value?

Thank you

Community
  • 1
  • 1
nani
  • 382
  • 5
  • 15

1 Answers1

2

the problem is, that Gson does not know how to map "latitude_deg" to latitudeDeg. You need to provide annotations as in the following example:

private class Airport {

    @SerializedName("latitude_deg")
    private double latitudeDeg;

    @SerializedName("longitude_deg")
    private double longitudeDeg;

    @SerializedName("elevation_ft")
    private int elevationFt;

    @SerializedName("continent")
    private String continent;

    @SerializedName("iso_country")
    private String isoCountry;

    // get-/ set-methods
}

@Test
public void name() throws Exception {
    Gson gson = new Gson();

    String jsonInString = "{ \"latitude_deg\": 40.07, \"longitude_deg\": -74.07, \"elevation_ft\": 11, \"continent\": NA, \"iso_country\": \"US\" }";
    Airport staff = gson.fromJson(jsonInString, Airport.class);

    Assert.assertTrue("US".equals(staff.getIsoCountry()));
    Assert.assertTrue("NA".equals(staff.getContinent()));
    Assert.assertTrue(staff.getElevationFt() == 11);
    Assert.assertTrue(40.07d == staff.getLatitudeDeg());
    Assert.assertTrue(-74.07d == staff.getLongitudeDeg());
}
Sergej Isbrecht
  • 3,842
  • 18
  • 27
  • I use your code but it gives me some error: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Invalid double: "", why? Could I download it as a String before? – nani Nov 07 '16 at 15:30
  • Could you please post the json, which will throw the exception? – Sergej Isbrecht Nov 07 '16 at 15:38
  • Json file is the same I've post in the question, simply add the [{ ... }] – nani Nov 07 '16 at 16:06
  • Well, if you hit the api-endpoint, you will get more than one json-item back, are you? I would guess there is something wrong with the json-string. I would need the full json or the stacktrace. – Sergej Isbrecht Nov 07 '16 at 16:23
  • could I send you a pm? – nani Nov 07 '16 at 16:27
  • Sure, if you know how to do it. I don't know how to do it. – Sergej Isbrecht Nov 07 '16 at 16:31
  • finally I've solved, putting on all types "String": the problem is a Number value that become " " in some fields...thank you for your help! – nani Nov 07 '16 at 22:28