【发布时间】:2023-03-21 01:15:01
【问题描述】:
我正在尝试从 URL 读取 JSON 数据,目前我在 MainActivity 类的 onCreate 方法中获得了这段代码 sn-p:
try {
URL url = new URL("https://bugzilla.mozilla.org/rest/bug/35");
try{
InputStream in = url.openStream();
JsonReader reader = new JsonReader(new InputStreamReader(in));
// more will be there...
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
当我在虚拟设备上运行应用程序时,它说程序已停止,甚至无法启动。 也许我试图以错误的方式读取数据。 最重要的是,我想从 URL 读取 JSON 数据。我还在应用程序中添加了 INTERNET 权限,但没有帮助。
【问题讨论】:
-
堆栈跟踪。阅读它。
-
你是在 UI 线程上运行这个吗?