Answer by SMBiggs for How can I fix 'android.os.NetworkOnMainThreadException'?
This is a big question, hence four pages of answers (many are old and should be culled)! Yet no one has yet mentioned the current google-approved kotlin solution for code within an Activity (or...
View ArticleAnswer by rajeev ranjan for How can I fix...
The android.os.NetworkOnMainThreadException is thrown when an application attempts toperform network operations on its main thread. This exception is specificallydesigned to prevent the main thread,...
View ArticleAnswer by Sourav Kumar Verma for How can I fix...
Executors.newFixedThreadPool(3).execute(() -> { //DO Task; });
View ArticleAnswer by programandoconro for How can I fix...
I solved using Thread in Kotlin. There are many examples using Java, so I wanted to add a solution that worked for me in Kotlin. Thread { println("NEW THREAD") callAPI() // add your own task...
View ArticleAnswer by Alex Mamo for How can I fix 'android.os.NetworkOnMainThreadException'?
For Kotlin users who don't want to use coroutines, it will be as simple as:Thread { //Run the long-time operation. val serverResponse = getResponseFromServer() runOnUiThread { //Display the result on...
View ArticleHow can I fix 'android.os.NetworkOnMainThreadException'?
I got an error while running my Android project for RssReader. Code:URL url = new URL(urlToRssFeed);SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser parser =...
View ArticleAnswer by Mark Allison for How can I fix...
You cannot perform network I/O on the UI thread on Honeycomb. Technically, it is possible on earlier versions of Android, but it is a really bad idea as it will cause your app to stop responding, and...
View ArticleAnswer by Michael Spector for How can I fix...
NOTE : AsyncTask was deprecated in API level 30.AsyncTask | Android DevelopersThis exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code...
View ArticleAnswer by user1169115 for How can I fix...
You should almost always run network operations on a thread or as an asynchronous task.But it is possible to remove this restriction and you override the default behavior, if you are willing to accept...
View ArticleAnswer by Piyush-Ask Any Difference for How can I fix...
You disable the strict mode using following code:if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();...
View ArticleAnswer by Dipak Keshariya for How can I fix...
There are two solutions of this problem.Don't use a network call in the main UI thread. Use an async task for that.Write the below code into your MainActivity file after...
View ArticleAnswer by Dr.Luiji for How can I fix 'android.os.NetworkOnMainThreadException'?
I solved this problem using a new Thread.Thread thread = new Thread(new Runnable() { @Override public void run() { try { //Your code goes here } catch (Exception e) { e.printStackTrace(); }...
View ArticleAnswer by sivag1 for How can I fix 'android.os.NetworkOnMainThreadException'?
The top answer of spektom works perfect.If you are writing the AsyncTask inline and not extending as a class, and on top of this, if there is a need to get a response out of the AsyncTask, one can use...
View ArticleAnswer by venergiac for How can I fix 'android.os.NetworkOnMainThreadException'?
Do not use strictMode (only in debug mode)Do not change SDK versionDo not use a separate threadUse Service or AsyncTaskSee also Stack Overflow question:android.os.NetworkOnMainThreadException sending...
View ArticleAnswer by raihan ahmed for How can I fix...
This happens in Android 3.0 and above. From Android 3.0 and above, they have restricted using network operations (functions that access the Internet) from running in the main thread/UI thread (what...
View ArticleAnswer by rharvey for How can I fix 'android.os.NetworkOnMainThreadException'?
For me it was this:<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />The device I was testing my app on was 4.1.2 which is SDK Version 16!Make the sure the target version is the...
View ArticleAnswer by henry4343 for How can I fix 'android.os.NetworkOnMainThreadException'?
Do the network actions on another thread.For example:new Thread(new Runnable(){ @Override public void run() { // Do network action in this function }}).start();And add this to file...
View ArticleAnswer by Kapil Vats for How can I fix...
You should not do any time-consuming task on the main thread (UI thread), like any network operation, file I/O, or SQLite database operations. So for this kind of operation, you should create a worker...
View ArticleAnswer by Dhruv Jindal for How can I fix...
Network-based operations cannot be run on the main thread. You need to run all network-based tasks on a child thread or implement AsyncTask.This is how you run a task in a child thread:new Thread(new...
View ArticleAnswer by Stevie for How can I fix 'android.os.NetworkOnMainThreadException'?
The accepted answer has some significant downsides. It is not advisable to use AsyncTask for networking unless you really know what you are doing. Some of the down-sides include:AsyncTask's created as...
View Article