【问题标题】:android trouble getting a inner list parameter from gsonandroid从gson获取内部列表参数时遇到问题
【发布时间】:2019-02-20 06:34:46
【问题描述】:

所以我尝试使用改造来解析的 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 方法。

有人有想法吗?

【问题讨论】:

  • 您的代码显示的值是多少?
  • json代码的整个天气部分,我想要天气部分里面的描述部分

标签: android json parsing gson retrofit


【解决方案1】:

您必须更改您的WeatherParams。在您提供的 json 模型中,字段 weather 是一个数组,而在您当前的 WeatherParams 类中,它是一个对象。你的 WeatherParams 类应该是这样的

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

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

【讨论】:

  • 无法获取。你写的应该是这样的,所以我不明白错误在哪里
  • 该字段的 getter 和 setter 怎么样 - 我应该如何重写它?
  • 好的,getter 和 setter 正确,文本正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
相关资源
最近更新 更多