Hi, can anyone give me some advice? As part of my ...
# android
m
Hi, can anyone give me some advice? As part of my learning I decided to create a simple application working with the openWeather API. But I ran into a problem regarding the request.
Copy code
val url = "<https://api.openweathermap.org/data/2.5/forecast/daily?q=hamburg,de&appid=33aa634c216259f797f35e862f07****>"
val resultJson = URL(url).readText()
Log.d("Weather Report", resultJson)
val jsonObj = JSONObject(resultJson)
val main = jsonObj.getJSONObject("main")
Attempting to execute then brings up the following: https://pastebin.com/84EDhwyU The debug basically did not detect any faults. Anyway, the bug refers this line:
Copy code
val resultJson = URL(url).readText()
Edit: I will add that when I played with it yesterday I somehow mysteriously managed to get the resulting JSON, but only once and without any code modification. It's as if each time the resulting JSON was loaded incorrectly
c
looking at your stacktrace:
Caused by: android.os.NetworkOnMainThreadException
You cannot perform network requests on the main thread. Consider shifting the request to a background thread