Answer by Richard Kamere for How do I fix...
I had a similar problem, I just used the following in oncreate method of your activity. //allow strict mode StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();...
View ArticleAnswer by Vipul Prajapati for How do I fix...
Do this in Background Thread using AsycTask Java class NetworkThread extends AsyncTask<String, Void, String> { protected Void doInBackground(String... arg0) { //Your implementation } protected...
View ArticleAnswer by Sazzad Hissain Khan for How do I fix...
Kotlin version internal class RetrieveFeedTask : AsyncTask<String, Void, RSSFeed>() { override fun doInBackground(vararg urls: String): RSSFeed? { try { // download // prepare RSSFeeds return...
View ArticleAnswer by Santanu Sur for How do I fix...
You can use Kotlin-coroutines class YoutActivity : AppCompatActivity, CoroutineScope { override fun onCreate(...) { launch { yourHeavyMethod() } } suspend fun yourHeavyMethod() { with(Dispatchers.IO){...
View ArticleAnswer by Devix for How do I fix 'android.os.NetworkOnMainThreadException'?
If you are working in kotlin and anko you can add doAsync { method() }
View ArticleAnswer by Ashok Kumar for How do I fix...
Different options: use normal java runnable thread to process network task and can use runOnUIThread() to update the UI intentservice/ async task can be used in case you want to update the UI after...
View ArticleAnswer by yoAlex5 for How do I 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 ArticleAnswer by Elye for How do I 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 ArticleAnswer by KG6ZVP for How do I 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 ArticleAnswer by sharath kumar for How do I 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 ArticleAnswer by Rahul for How do I 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 ArticleAnswer by J.D.1731 for How do I 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.html Anko supported library for...
View ArticleAnswer by Shinoo Goyal for How do I fix...
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 ArticleAnswer by Ravindra babu for How do I 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 ArticleAnswer by Hossain Ahamed for How do I 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 ArticleAnswer by aks for How do I 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 operation...
View ArticleAnswer by Himanshu for How do I 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 ArticleAnswer by Mansuu.... for How do I 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 ArticleAnswer by Lovekush Vishwakarma for How do I fix...
How to fix android.os.NetworkOnMainThreadException What is NetworkOnMainThreadException: In Android all the UI operations we have to do on the UI thread (main thread). If we perform background...
View ArticleAnswer by Alex Shutov for How do I fix...
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