android从gson获取内部列表参数时遇到麻烦

问题描述 投票:0回答:2

所以我试图使用改造解析的json URL是:https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22

我的MainActivity java文件代码如下:

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) {

        }
    });
}

我的api接口代码是:

public interface Api {

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

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

}

我的WeatherParams类是:

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 + '\'' +
                    "}";
}

}

最后我的WeatherItem类对象是:

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

@Override
public String toString(){
    return 
        "WeatherItem{" + 
        "icon = '" + icon + '\'' + 
        ",description = '" + description + '\'' + 
        ",main = '" + main + '\'' + 
        ",id = '" + id + '\'' + 
        "}";
    }

我想要的是WeatherItem上的getDescription方法。

我将一个字符串设置为等于response.body(),之后我将textview设置为等于该字符串,但该字符串包含一个天气项列表,因此我无法访问单个weatherItem的getDescription方法。

有人有想法吗?

android json parsing gson retrofit
2个回答
0
投票

你必须改变你的WeatherParams。在你提供的json模型中,字段weather是一个数组,而在你当前的WeatherParams类中它是一个单独的对象。您的WeatherParams类应该如下所示

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

然后你可以通过getDescription访问它所需的任何元素来调用WeatherParams.getWeather().get(itemIndex)函数


0
投票

现在我有另一个问题,我使用了这段代码:

public void onResponse(Call<WeatherParams> call, Response<WeatherParams> response) {
                final WeatherParams body = response.body();
                textView.setText(body.getWeather().get(0).getDescription());

            }

在没有声明特定列表索引的情况下获取描述是一种更通用的方法吗?因为列表索引可能会随时间而变化。

© www.soinside.com 2019 - 2024. All rights reserved.