We are writing an android app which loads some data from remote database using a URL. While doing so we encountered network on main thread exception. What did we do wrong and how can we handle it?
Asked
7 years ago
Viewed
653 times
We are writing an android app which loads some data from remote database using a URL. While doing so we
encountered network on main thread exception. What did we do wrong and how can we handle it?
1 Answer
This exception is thrown since the user tries to connect to the database on the main thread. since network connections are to be handled in the background because they take heavy load which causes the main thread to work more than usual and raises an exception. You need to do these network connections in the separate thread that is background thread. To do this you can use AyncTask class, SyncAdapter or other libraries such as okHttp, etc.