简体   繁体   English

无法使用 Android 从 URL 读取 JSON 数据

[英]Cant read JSON data from URL with Android

I'm trying to read JSON data form a URL, and currently i got this code snippet inside my onCreate method in my MainActivity class:我正在尝试从 URL 读取 JSON 数据,目前我在 MainActivity 类的 onCreate 方法中获取了此代码片段:

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();
    }

When I run the app on a virtual device, it says that the program is stopped, it can't even start it.当我在虚拟设备上运行该应用程序时,它说该程序已停止,甚至无法启动它。 Maybe I'm trying to read the data in a wrong way.也许我试图以错误的方式读取数据。 The most important thing, is that I want to read JSON data from URL.最重要的是,我想从 URL 读取 JSON 数据。 I also added the INTERNET permission to the aplication, but it didn't help.我还向应用程序添加了 INTERNET 权限,但没有帮助。

Use AsyncTask to perform network operation and For connecting to url use UrlConnection .使用AsyncTask执行网络操作,连接到 url 使用UrlConnection
for example see this thread例如看到这个线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM