You can also resolve this issue by using Strict Mode using the below code. It's also an alternative to resolving this issue.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
But the best practice would be to use AsyncTask.