gpt4 book ai didi

java - openweathermap API 有方括号中的部分,无法在 Android Studio 中调用该 json 数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:14 25 4
gpt4 key购买 nike

所以我正在尝试制作一个天气应用程序,API 可以工作,但仅在某些区域有效。现在,我只是尝试对 API 进行 Toast,以确保它可以正常工作,然后再继续执行应用程序的其余部分。

public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();

OpenWeatherMapClient api = retrofit.create(OpenWeatherMapClient.class);
Call<DailyWeather> call = api.getDailyWeather();
call.enqueue(new Callback<DailyWeather>()
{
@Override
public void onResponse(Call<DailyWeather> call, Response<DailyWeather> response)
{
Toast.makeText(MainActivity.this, (response.body().getWeather().getDescription()).toString(), Toast.LENGTH_SHORT).show();
for( Weather w : response.body().getWeather())


{
Log.d("Weather", w.getId().toString());
Log.d("Weather", w.getMain().toString());
Log.d("Weather", w.getDescription().toString());
}

}

所以这是我的 MainActivity.java,如果我用 .getCoord().getLat()) 替换 getWeather().getDescription()) 就可以了。我唯一注意到的是天气的 API 部分位于方括号中。

因此,如果我运行 API 搜索“Birmingham,uk”,这就是 json(在通过在线解析器以便于阅读之后)

{
"coord":{
"lon":-1.9,
"lat":52.48
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03d"
}
],
"base":"stations",
"main":{
"temp":282.57,
"pressure":1008,
"humidity":76,
"temp_min":282.15,
"temp_max":283.15
},

For reference I'll leave the Coord and Weather classes I have in case the error is in there.

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Coord {

@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("lat")
@Expose
private Double lat;

public Double getLon() {
return lon;
}

public void setLon(Double lon) {
this.lon = lon;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

}

还有

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Weather {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;

public Integer getId() {
return id;
}

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

public String getMain() {
return main;
}

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

public String getDescription() {
return description;
}

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

public String getIcon() {
return icon;
}

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

}

以及请求的 DailyWeather 类

 public class DailyWeather {

@SerializedName("coord")
@Expose
private Coord coord;
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("base")
@Expose
private String base;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("visibility")
@Expose
private Integer visibility;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("dt")
@Expose
private Integer dt;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("cod")
@Expose
private Integer cod;

public Coord getCoord() {
return coord;
}

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

public List<Weather> getWeather() {
return weather;
}

public void setWeather(List<Weather> weather) {
this.weather = weather;
}

public String getBase() {
return base;
}

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

public Main getMain() {
return main;
}

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

public Integer getVisibility() {
return visibility;
}

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

public Wind getWind() {
return wind;
}

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

public Clouds getClouds() {
return clouds;
}

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

public Integer getDt() {
return dt;
}

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

public Sys getSys() {
return sys;
}

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

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

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

public Integer getCod() {
return cod;
}

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

最佳答案

天气变量是一个列表,因此要访问它,您应该执行以下操作:

List<Weather> weatherList = response.body().getWeather();
if(weatherList!=null && !weatherList.isEmpty()){
Toast.makeText(MainActivity.this, (weatherList.get(0).getDescription()).toString(), Toast.LENGTH_SHORT).show();
}

您无法执行 getWeather().getDescription(),因为 getWeather() 返回一个列表。因此,您需要在列表中选择一个元素,然后才执行 getDescription()

关于java - openweathermap API 有方括号中的部分,无法在 Android Studio 中调用该 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com