Quantcast
Channel: How can I fix 'android.os.NetworkOnMainThreadException'? - Stack Overflow
Browsing all 191 articles
Browse latest View live

Answer by Santanu Sur for How to fix 'android.os.NetworkOnMainThreadException'?

You can use Kotlin-coroutines class YoutActivity : AppCompatActivity, CoroutineScope { override fun onCreate(...) { launch { yourHeavyMethod() } } suspend fun yourHeavyMethod() { with(Dispatchers.IO){...

View Article


Answer by Devix for How to fix 'android.os.NetworkOnMainThreadException'?

If you are working in kotlin and anko you can add doAsync { method() }

View Article


Answer by Ashok Kumar for How to fix 'android.os.NetworkOnMainThreadException'?

Different options:use normal java runnable thread to process network task and can use runOnUIThread() to update the UIintentservice/ async task can be used in case you want to update the UI after...

View Article

Answer by yoAlex5 for How to fix 'android.os.NetworkOnMainThreadException'?

You are able to move a part of your code into another thread to offload the main thread and avoid getting ANR, NetworkOnMainThreadException, IllegalStateException(e.g. Cannot access database on the...

View Article

Answer by Elye for How to fix 'android.os.NetworkOnMainThreadException'?

As of 2018, I would recommend to use RxJava in Kotlin for network fetching. A simple example is below.Single.fromCallable { // Your Network Fetching Code Network.fetchHttp(url) }...

View Article


Answer by KG6ZVP for How to fix 'android.os.NetworkOnMainThreadException'?

There are many great answers already on this question, but a lot of great libraries have come out since those answers were posted. This is intended as a kind of newbie-guide.I will cover several use...

View Article

Answer by sharath kumar for How to fix...

The main thread is the UI thread, and you cannot do an operation in the main thread which may block the user interaction. You can solve this in two ways:Force to do the task in the main thread like...

View Article

Answer by Rahul for How to fix 'android.os.NetworkOnMainThreadException'?

Never do any long running work on UI thread, that long running work can be communication with server, read/write on file etc. These tasks should be on background thread, thats why Service, AsyncTask,...

View Article


Answer by J.D.1731 for How to fix 'android.os.NetworkOnMainThreadException'?

You can either use KOTLIN and ANKO. Kotlin is new official language for Android more about it you can find here https://kotlinlang.org/docs/tutorials/kotlin-android.htmlAnko supported library for...

View Article


Answer by Shinoo Goyal for How to fix 'android.os.NetworkOnMainThreadException'?

RxAndroid is another better alternative to this problem and it saves us from hassles of creating threads and then posting results on Android UI thread. We just need to specify threads on which tasks...

View Article

Answer by Ravindra babu for How to fix...

New Thread and AsyncTask solutions have been explained already.AsyncTask should ideally be used for short operations. Normal Thread is not preferable for Android. Have a look at alternate solution...

View Article

Answer by Hossain Ahamed for How to fix...

You can not call network on the main thread or UI thread. On Android if you want to call network there are two options -Call asynctask, which will run one background thread to handle the network...

View Article

Answer by aks for How to fix 'android.os.NetworkOnMainThreadException'?

As Android is working on a single thread, you should not do any network operation on the main thread. There are various ways to avoid this.Use the following way to perform a network...

View Article


Answer by Himanshu for How to fix 'android.os.NetworkOnMainThreadException'?

You can also resolve this issue by using Strict Mode using the below code. It's also an alternative to resolving this issue.StrictMode.ThreadPolicy policy = new...

View Article

Answer by Mansuu.... for How to fix 'android.os.NetworkOnMainThreadException'?

android.os.NetworkOnMainThreadException is thrown when network operations are performed on the main thread. You better do this in AsyncTask to remove this Exception. Write it this way: new...

View Article


Answer by Lovekush Vishwakarma for How to fix...

How to fix android.os.NetworkOnMainThreadExceptionWhat is NetworkOnMainThreadException:In Android all the UI operations we have to do on the UI thread (main thread). If we perform background operations...

View Article

Answer by Alex Shutov for How to fix 'android.os.NetworkOnMainThreadException'?

There is another very convenient way for tackling this issue - use rxJava's concurrency capabilities. You can execute any task in background and post results to main thread in a very convenient way, so...

View Article


Answer by bpr10 for How to fix 'android.os.NetworkOnMainThreadException'?

We can also use RxJava to move network operations to a background thread. And it's fairly simple as well.webService.doSomething(someData) .subscribeOn(Schedulers.newThread())-- This for background...

View Article

Answer by BalaramNayak for How to fix 'android.os.NetworkOnMainThreadException'?

The NetworkOnMainThread exception occurs because you have called some network operation on the default thread, that is, the UI thread. As per Android version Android 3 (Honeycomb) which is not allowed,...

View Article

Answer by Adnan Abdollah Zaki for How to fix...

This exception is thrown when an application attempts to perform a networking operation on its main thread. If your task took above five seconds, it takes a force close.Run your code in AsyncTask:class...

View Article
Browsing all 191 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>