我想用
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val json = URL("https://my-api-url.com/something").readText()
simpleTextView.setText(json)
}
但是发生了这个致命错误
FATAL EXCEPTION: main
Process: com.mypackage.randompackage, PID: 812
java.lang.RuntimeException: Unable to start activity ComponentInfo{ ***.MainActivity}: android.os.NetworkOnMainThreadException
如何简单地从 URL 链接读取 JSON?该async
函数的包不存在。