从json(kotlin android)获取天气结果

时间:2018-08-17 17:20:34

标签: android json kotlin

我正在尝试从json中获取一些结果,但是我有一个问题。当我只有这一行代码时,它可以工作并且我可以获取温度

var main: TemperatureData? = null

但是我也想从json中获取更多值,所以我插入这行代码

var weather: WeatherDataJson? = null

当我添加第二行时,它不会获取任何数据(它甚至停止获取var main:... data)

这是我的天气数据类

class WeatherData {
    var main: TemperatureData? = null
    var weather: WeatherDataJson? = null

}

这是我的界面类

interface ApiInterface {
    @GET("data/2.5/weather?q=Prague")
    fun getWeatherData(@Query("appid") appId: String)
            : Call<WeatherData>

}

我的TemperatureData类

data class TemperatureData(var temp: String)

还有我的WeatherDataJson类

data class WeatherDataJson(var description:String)

在我的主类中,我具有一个将json显示到屏幕上的功能,但是我无法加载“ var weather ...” 这是函数

private fun getTemperatureData(repository: Repository) {
        repository.getApiInterface()
                .getWeatherData("4cf7f6610d941a1ca7583f50e7e41ba3")
                .enqueue(object : Callback<WeatherData> {
                    override fun onFailure(call: Call<WeatherData>?, t: Throwable?) {
                        t?.printStackTrace()
                    }

                    override fun onResponse(call: Call<WeatherData>?, response: Response<WeatherData>?) {
                        val weatherData: WeatherData? = response?.body()
                        weatherData?.let {
                            it.main?.let {
                                tempText.text = it.temp
                            }
                            it.weather?.let{
                                weatherTextFromApi.text=it.description
                            }
                        }
                    }
                })
    }

说明上显示未解决的参考说明

2 个答案:

答案 0 :(得分:1)

如果您使用openweathermap api声明这些数据类

#include <iostream>
#include <string>

using namespace std;


int main()
{
    std::cout<<bob; // bob is not declared
}

In function 'int main()':
9:16: error: 'bob' was not declared in this scope

如果您的目标是从天气中获取描述,那么您所需要的就是

data class WeatherData(
    @SerializedName("coord") val coord: Coord,
    @SerializedName("weather") val weather: List<Weather>,
    @SerializedName("base") val base: String,
    @SerializedName("main") val main: TemperatureData,
    @SerializedName("visibility") val visibility: Int,
    @SerializedName("wind") val wind: Wind,
    @SerializedName("clouds") val clouds: Clouds,
    @SerializedName("dt") val dt: Int,
    @SerializedName("sys") val sys: Sys,
    @SerializedName("id") val id: Int,
    @SerializedName("name") val name: String,
    @SerializedName("cod") val cod: Int
)

data class Sys(
    @SerializedName("type") val type: Int,
    @SerializedName("id") val id: Int,
    @SerializedName("message") val message: Double,
    @SerializedName("country") val country: String,
    @SerializedName("sunrise") val sunrise: Int,
    @SerializedName("sunset") val sunset: Int
)

data class Coord(
    @SerializedName("lon") val lon: Double,
    @SerializedName("lat") val lat: Double
)

data class TemperatureData(
    @SerializedName("temp") val temp: Double,
    @SerializedName("pressure") val pressure: Int,
    @SerializedName("humidity") val humidity: Int,
    @SerializedName("temp_min") val tempMin: Double,
    @SerializedName("temp_max") val tempMax: Double
)

data class Weather(
    @SerializedName("id") val id: Int,
    @SerializedName("main") val main: String,
    @SerializedName("description") val description: String,
    @SerializedName("icon") val icon: String
)

data class Clouds(
    @SerializedName("all") val all: Int
)

data class Wind(
    @SerializedName("speed") val speed: Double,
    @SerializedName("deg") val deg: Int
)

用于获取图标的网址

这有点棘手,您可以从响应中获取网址,但只能获取图标的网址。

weatherData.weather.firstOrNull()?. description ?: ""

之后,您应该使用Glide库或Picasso将imageUrl加载到ImageView

https://github.com/bumptech/glide上查看有关Glide的更多信息

答案 1 :(得分:0)

OpenWeatherMap的服务器响应正在返回weather的JSON数组,而不是JSON对象。 Gson / Retrofit无法自动将数组变成单个对象,因此您需要更改数据类以保存列表(或使用自定义Gson解串器并手动从数组中获取所需的项)。

顶级课程应为的示例:

class WeatherData {
    var main: TemperatureData? = null
    var weather: List<WeatherDataJson>? = null
}