[java] How to fix 'android.os.NetworkOnMainThreadException'?

These answers need to be updated to use more contemporary way to connect to servers on Internet and to process asynchronous tasks in general, e.g. you can find examples where Tasks are used in Google Drive API sample. The same should be used in this case. I'll use OP's original code to demonstrate this approach.

First, you'll need to define an off-main thread executor and you need to do it only once:

        private val mExecutor: Executor = Executors.newSingleThreadExecutor()

Then process your logic in that executor, which will be running off main thread

        Tasks.call (mExecutor, Callable<String> {

                val url = URL(urlToRssFeed)
                val factory = SAXParserFactory.newInstance()
                val parser = factory.newSAXParser()
                val xmlreader = parser.getXMLReader()
                val theRSSHandler = RssHandler()
                xmlreader.setContentHandler(theRSSHandler)
                val is = InputSource(url.openStream())
                xmlreader.parse(is)
                theRSSHandler.getFeed()
                // complete processing and return String or other object
                // e.g. you could return Boolean indicating a success or failure
                return@Callable someResult
        }).continueWith{
            // it.result here is what your asynchronous task has returned
            processResult(it.result)
        }

continueWith clause will be executed after your asynchronous task is completed and you will have an access to a value that has been returned by the task through it.result.

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-networking

Trusting all certificates with okHttp Comparison of Android Web Service and Networking libraries: OKHTTP, Retrofit and Volley Broadcast receiver for checking internet connection in android app Android check internet connection How to fix 'android.os.NetworkOnMainThreadException'?

Examples related to networkonmainthread

Android - android.os.NetworkOnMainThreadException How to fix 'android.os.NetworkOnMainThreadException'?