There are two solutions of this problem.
Don't use a network call in the main UI thread. Use an async task for that.
Write the below code into your MainActivity file after setContentView(R.layout.activity_main);:
if (android.os.Build.VERSION.SDK_INT > 9) {StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);}
And the below import statement into your Java file.
import android.os.StrictMode;