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 votes
137 views
Yakshit Agarwal asked 22-Jan-2018 in Technology by Yakshit Agarwal

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

0 votes
Prateek sharma answered 22-Jan-2018 by Prateek sharma
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.