android.os.NetworkOnMainThreadException . Need to use async task?

I guess you are trying to peform some Network operation on your main thread

NetworkOnMainThreadException from the Docs

The exception that is thrown when an application attempts to perform a
networking operation on its main thread.


Its Better to use AsyncTask

private class MyAsyncTask extends AsyncTask<Void, Void, Void>

        ProgressDialog mProgressDialog;
        protected void onPostExecute(Void result) {

        protected void onPreExecute() {
            mProgressDialog =, 
                                            "Loading...", "Data is Loading...");

        protected Void doInBackground(Void... params) {
           // your network operation
            return null;

Leave a Comment