質問編集履歴
1
WeatherGetjsonクラスの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -152,30 +152,68 @@
|
|
152
152
|
```WeatherGetjson
|
153
153
|
package com.example.weatherapp
|
154
154
|
|
155
|
+
import kotlinx.coroutines.Dispatchers
|
156
|
+
import kotlinx.coroutines.GlobalScope
|
157
|
+
import kotlinx.coroutines.launch
|
158
|
+
import kotlinx.coroutines.withContext
|
159
|
+
import org.json.JSONException
|
155
160
|
import org.json.JSONObject
|
156
161
|
import java.io.BufferedReader
|
162
|
+
import java.io.IOException
|
157
163
|
import java.io.InputStreamReader
|
158
164
|
|
159
165
|
import java.net.URL
|
160
166
|
|
161
|
-
|
162
|
-
fun getJson(): String {
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
167
|
+
class WeatherGetjson() {
|
168
|
+
|
168
|
-
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
suspend fun weatherTask() {
|
173
|
+
|
169
|
-
val apiKey = "//
|
174
|
+
val apiKey = " //APIKey"
|
170
175
|
val apiUrl =
|
171
176
|
"https://api.openweathermap.org/data/2.5/onecall?lat=35.68&lon=139.70&exclude=current,minutely,hourly,alerts&appid=${apiKey}"
|
177
|
+
|
178
|
+
GlobalScope.launch {
|
179
|
+
val result = weatherBackgroundTast(apiUrl)
|
180
|
+
weatherJsonTask(result)
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
private suspend fun weatherBackgroundTast(apiUrl: String): String {
|
186
|
+
|
187
|
+
val response = withContext(Dispatchers.IO) {
|
188
|
+
|
189
|
+
var weatherdata = ""
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
try {
|
172
|
-
val url = URL(apiUrl)
|
194
|
+
val url = URL(apiUrl)
|
173
|
-
val br = BufferedReader(InputStreamReader(url.openStream()))
|
195
|
+
val br = BufferedReader(InputStreamReader(url.openStream()))
|
174
|
-
val str = br.readText()
|
196
|
+
val str = br.readText()
|
197
|
+
|
198
|
+
} catch (e: IOException) {
|
199
|
+
e.printStackTrace()
|
200
|
+
} catch (e: JSONException) {
|
201
|
+
e.printStackTrace()
|
202
|
+
}
|
203
|
+
return@withContext weatherdata
|
204
|
+
|
205
|
+
}
|
206
|
+
return response
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
private fun weatherJsonTask(result: String) {
|
211
|
+
|
212
|
+
//取得した情報をしまう
|
213
|
+
var list: ArrayList<String> = arrayListOf()
|
214
|
+
|
175
|
-
val json = JSONObject(st
|
215
|
+
val json = JSONObject(result)
|
176
216
|
val weatherAry = json.getJSONArray("daily")
|
177
|
-
|
178
|
-
var list: ArrayList<String> = arrayListOf()
|
179
217
|
|
180
218
|
|
181
219
|
for (i in 0..7) {
|
@@ -191,10 +229,13 @@
|
|
191
229
|
var max = temp.get("max")
|
192
230
|
|
193
231
|
list += (arrayListOf(date, icon, pop, min, max).toString())
|
194
|
-
weatherdata = i.toString()
|
195
|
-
}
|
232
|
+
}
|
196
|
-
|
233
|
+
|
234
|
+
|
235
|
+
|
197
|
-
}
|
236
|
+
}
|
237
|
+
|
238
|
+
|
198
239
|
|
199
240
|
|
200
241
|
```
|