所以我正在尝试创建一个天气应用程序,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部分在sqaure括号中。
因此,如果我运行我的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
},
作为参考,我将保留我所拥有的Coord和Weather课程 错误就在那里。
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;
}
}
答案 0 :(得分:0)
天气变量是一个列表,所以要访问它,你应该这样做:
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()
。