You're performing this operation in the main UI, which is forbidden in Android because it would block the thread until it was processed, resulting probably in an Application Not Responding.
Move the network operation into an AsyncTask
. Put the code in the doInBackground()
method, as when it will be run, it will automatically create another thread and perform the network operation there.