简体   繁体   中英

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:

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. I also added the INTERNET permission to the aplication, but it didn't help.

Use AsyncTask to perform network operation and For connecting to url use UrlConnection .
for example see this thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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