문제

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.

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top