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

2
  • So, your problem is, that the values are not filled? Commented Nov 7, 2016 at 14:33
  • @Hans Wurst it not takes the values from the Json Commented Nov 7, 2016 at 14:39

1 Answer 1

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()); } 
Sign up to request clarification or add additional context in comments.

7 Comments

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?
Could you please post the json, which will throw the exception?
Json file is the same I've post in the question, simply add the [{ ... }]
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.
could I send you a pm?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.