Internet communication must run in a separate thread.
Use AsyncTask to simplify this.
You can read more about threads in Android here
EDIT: If you really want to access the internet on the main thread, you can allow it in the onCreate()
method with the following code:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
However, I really wouldn't recommend this approach.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…