Ask Your Question
4

What is causing the android.os.NetworkOnMainThreadException to occur when making a response call using OkHttpClient() for a synchronous call?

asked 2023-06-12 06:38:15 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-12 07:07:02 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-12 06:38:15 +0000

Seen: 11 times

Last updated: Jun 12 '23