【问题标题】:Cant read JSON data from URL with Android无法使用 Android 从 URL 读取 JSON 数据
【发布时间】: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 线程上运行这个吗?

标签: java android json


【解决方案1】:

使用AsyncTask进行网络操作,连接到url使用UrlConnection
例如看到这个thread

【讨论】:

  • 非常感谢,这就是我想要的:)
猜你喜欢
  • 2013-10-04
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 2015-01-20
  • 2016-01-18
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多