Skip to content Skip to sidebar Skip to footer

Android.os.networkonmainthreadexception . Need To Use Async Task?

I'm having a problem with my android login functionality, getting android.os.NetworkOnMainThreadException I removed the password field just for now to test if just the username was

Solution 1:

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.

UPDATE:

Its Better to use AsyncTask

privateclassMyAsyncTaskextendsAsyncTask<Void, Void, Void>
    {

        ProgressDialog mProgressDialog;
        @OverrideprotectedvoidonPostExecute(Void result) {
            mProgressDialog.dismiss();
        }

        @OverrideprotectedvoidonPreExecute() {
            mProgressDialog = ProgressDialog.show(ActivityName.this, 
                                            "Loading...", "Data is Loading...");
        }

        @OverrideprotectedVoiddoInBackground(Void... params) {
           // your network operationreturnnull;
        }
    }

Solution 2:

Just change target version in manifest file to lover than Honeycomb.

<uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="8" />

Edit: But using AsyncTask is more convenient way to solve this issue.

Post a Comment for "Android.os.networkonmainthreadexception . Need To Use Async Task?"