This NetworkingOnMainThread exception is thrown when you perform network activity in the user interface thread. It has the potential to block the main thread, as it waits until the network connection is completed before resuming, thereby blocking the stream.
You have 2 options.
1 - Move all your networks to another stream. The most common and easiest solution is AsyncTask. But there are other options, such as Handlers . This is the recommended option.
2- You can change the policy to allow networking in the user interface stream.
just add this code
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
This parameter is not recommended because it simply bypasses the exception. An exception is thrown for a reason. I would choose the first option. There are many guides for implementing AsyncTask .
Good luck.
source share