You should never touch network from the main thread.
Implement AsyncTask for network operations.
private class MyInnerClass extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String params) {
return "Done";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
Call new MyInnerClass().execute(); from you main Activity and android will automatically call onPreExecute() do whatever you wana do inside this method before your network acess.
Android will then call doInBackground
Do you network related stuff inside doInBackground() and when finished it will automatically call onPostExecute() and the result will be passed as params to this method.