What will happen if HTTP Connection code is written in activity's Main thread?
i. It may lead to ANR.
ii. It wont give any error, but may lead to ANR till 2.3 version. but from 3.0 it is mandatory that all network connections has to happen in separate worker thread. else it will crash at run time with NetworkOnMainThreadException
iii. It works fine. No error will come.
iv. If we don't use separate background thread from 3.0 version on wards, system will throw run time exception.
ii & iii
ii & iv
The purpose of Main UI thread is to take care of all UI events coming to the screen from the user. All UI is under control of Main thread.
Connecting to internet is a heavy functionality. Basic android rule says don't do more than 1 heavy functionality in Main UI thread. Because it affects UI responsiveness and it may lead to ANR (Application Not Responding) error at run time.
Connecting to internet in main thread, wont give any error till 2.3 version but may lead to ANR at run time.
From 3.0 it is mandatory that all network connections has to happen in separate worker thread else it throws run time exception.
Exception name is : NetworkOnMainThreadException
Back To Top