Domanda

I made a SAX parser that accepts an InputStream as file to parse. If I use a local file it works, but if I try to get it from the internet it crashes when it opens the Input Stream.

There is the code:

    URL url = new URL(http://www.myDomain.com/xml/device.xml);
    URLConnection connection = url.openConnection();
    InputStream inStream = connection.getInputStream();

    ...

parser.parse(inStream, handler);

I have been trying it in several ways I have seen in other questions and it always crashes at InputStream inStream = connection.getInputStream(); or similar.

Thanks in advance.

È stato utile?

Soluzione

This might be NetworkOnMainThreadException you should not run costly operations like Network Connection on Main thread

Call that method in the following way

new Thread(new Runnable() {

        @Override
        public void run() {
            //Call your network connection method 
        }
    }).start();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top