Updated Answer:
Potentially long running operations such as network operations should be done in a worker thread.
The most effective way to create a worker thread for longer operations is with the AsyncTask class. Simply extend AsyncTask and implement the doInBackground() method to perform the work.
Many libraries are available to do network operations such as Volley, retrofit etc.
Old Answer:
Add following lines in your activity onCreate
method
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);