我正在使用“ OpenWeatherMap” API来获取天气预报。我收到了当天天气的回复。但是我不知道如何获得接下来三天的天气预报以及日期。
这是我当前的代码Json:
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
try {
/* Extracting JSON returns from the API */
val jsonObj = JSONObject(result)
val main = jsonObj.getJSONObject("main")
val sys = jsonObj.getJSONObject("sys")
val updatedAt:Long = jsonObj.getLong("dt")
val updatedAtText = "Updated at: "+ SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.ENGLISH).format(Date(updatedAt*1000))
val temp = main.getString("temp")+"°C"
val tempMin = "Min Temp: " + main.getString("temp_min")+"°C"
val tempMax = "Max Temp: " + main.getString("temp_max")+"°C"
答案 0 :(得分:0)
据我了解,您正在使用当前天气数据API来获取数据,它将仅返回当前日期数据。您可以使用以下API获得4天或7天的预测数据:
在android端处理JSON仅获得3天的数据。
答案 1 :(得分:0)
在Android Studio中,使用该插件将JSON转换为实体。
我将使用改造来获取数据列表。我只记得在上学的第一周使用AsyncTask。以后,我总是使用Retrofit来处理API,因为它节省了大量工作。这是Retrofit的gradle依赖项:
// Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.google.code.gson:gson:$gson_version"
implementation "com.squareup.retrofit2:converter-gson:$converter_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
在MVVM中,来自存储库的数据请求=> API服务=> ViewModel =>活动/片段。