Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The android.os.NetworkOnMainThreadException occurs when a network operation is performed on the main thread of the Android application. In the case of making a synchronous call using OkHttpClient(), the network operation is performed on the main thread, and if the operation takes too long, the application UI freezes, causing the ANR (Application Not Responding) error. To solve this issue, it is recommended to perform network operations on a separate thread using asynchronous methods such as enqueue() or execute() provided by OkHttpClient(). Alternatively, you can use AsyncTask or Kotlin Coroutines to offload network requests onto a separate thread to prevent the occurrence of NetworkOnMainThreadException.