As Android is working on a single thread, you should not do any network operation on the main thread. There are various ways to avoid this.
Use the following way to perform a network operation
- Asysnctask: For small operations which don't take much time.
- Intent Service: For network operation which take a big amount of time.
- Use a custom library like Volley and Retrofit for handling complex network operations
Never use StrictMode.setThreadPolicy(policy), as it will freeze your UI and is not at all a good idea.