android trouble getting a inner list parameter from gson

2402 views android
4

so the json URL I am trying to parse using retrofit is this: https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22

my MainActivity java file code is as the follows:

public class MainActivity extends AppCompatActivity {

TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.tvHelloWorld);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);

    Call<WeatherParams> call = api.getWeather();

    call.enqueue(new Callback<WeatherParams>() {
        @Override
        public void onResponse(Call<WeatherParams> call, Response<WeatherParams> response) {
            WeatherParams weatherParams = response.body();
            textView.setText(String.valueOf(weatherParams.getWeather()));

        }

        @Override
        public void onFailure(Call<WeatherParams> call, Throwable t) {

        }
    });
}

my api interface code is:

public interface Api {

String BASE_URL = "https://samples.openweathermap.org/data/2.5/";

@GET("weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22")
Call<WeatherParams> getWeather();

}

my WeatherParams class is:

public class WeatherParams {

@SerializedName("dt")
private int dt;

@SerializedName("coord")
private Coord coord;

@SerializedName("visibility")
private int visibility;

@SerializedName("weather")
private WeatherItem weather;

@SerializedName("name")
private String name;

@SerializedName("cod")
private int cod;

@SerializedName("main")
private Main main;

@SerializedName("clouds")
private Clouds clouds;

@SerializedName("id")
private int id;

@SerializedName("sys")
private Sys sys;

@SerializedName("base")
private String base;

@SerializedName("wind")
private Wind wind;

public void setDt(int dt) {
    this.dt = dt;
}

public int getDt() {
    return dt;
}

public void setCoord(Coord coord) {
    this.coord = coord;
}

public Coord getCoord() {
    return coord;
}

public void setVisibility(int visibility) {
    this.visibility = visibility;
}

public int getVisibility() {
    return visibility;
}

public void setWeather(WeatherItem weather) {
    this.weather = weather;
}

public WeatherItem getWeather() {
    return weather;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setCod(int cod) {
    this.cod = cod;
}

public int getCod() {
    return cod;
}

public void setMain(Main main) {
    this.main = main;
}

public Main getMain() {
    return main;
}

public void setClouds(Clouds clouds) {
    this.clouds = clouds;
}

public Clouds getClouds() {
    return clouds;
}

public void setId(int id) {
    this.id = id;
}

public int getId() {
    return id;
}

public void setSys(Sys sys) {
    this.sys = sys;
}

public Sys getSys() {
    return sys;
}

public void setBase(String base) {
    this.base = base;
}

public String getBase() {
    return base;
}

public void setWind(Wind wind) {
    this.wind = wind;
}

public Wind getWind() {
    return wind;
}

@Override
public String toString() {
    return
            "WeatherParams{" +
                    "dt = '" + dt + '\'' +
                    ",coord = '" + coord + '\'' +
                    ",visibility = '" + visibility + '\'' +
                    ",weather = '" + weather + '\'' +
                    ",name = '" + name + '\'' +
                    ",cod = '" + cod + '\'' +
                    ",main = '" + main + '\'' +
                    ",clouds = '" + clouds + '\'' +
                    ",id = '" + id + '\'' +
                    ",sys = '" + sys + '\'' +
                    ",base = '" + base + '\'' +
                    ",wind = '" + wind + '\'' +
                    "}";
}

}

and finally my WeatherItem class object is:

public class WeatherItem{

@SerializedName("icon")
private String icon;

@SerializedName("description")
private String description;

@SerializedName("main")
private String main;

@SerializedName("id")
private int id;

public void setIcon(String icon){
    this.icon = icon;
}

public String getIcon(){
    return icon;
}

public void setDescription(String description){
    this.description = description;
}

public String getDescription(){
    return description;
}

public void setMain(String main){
    this.main = main;
}

public String getMain(){
    return main;
}

public void setId(int id){
    this.id = id;
}

public int getId(){
    return id;
}

@

answered question

what is the value being displayed as a result of your code?

the entire Weather section of the json code, and I want the description part inside the weather section

1 Answer

11

You have to change your WeatherParams. In the json model your provided, the field weather is an array while in your current WeatherParams class it's a single object. Your WeatherParams class should look like this

private class WeatherParams{
   //other members 
   @SerializedName("weather")
   private WeatherItem weather;
}

Then you can call the getDescription function of any element you need by accessing it through WeatherParams.getWeather().get(itemIndex)

posted this

Have an answer?

JD

Please login first before posting an answer.