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

前端 未结 2 1001
萌比男神i
萌比男神i 2020-11-29 11:52

I\'m having a problem with my android login functionality, getting android.os.NetworkOnMainThreadException

I removed the password field just for now to

相关标签:
2条回答
  • 2020-11-29 12:11

    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

    private class MyAsyncTask extends AsyncTask<Void, Void, Void>
        {
    
            ProgressDialog mProgressDialog;
            @Override
            protected void onPostExecute(Void result) {
                mProgressDialog.dismiss();
            }
    
            @Override
            protected void onPreExecute() {
                mProgressDialog = ProgressDialog.show(ActivityName.this, 
                                                "Loading...", "Data is Loading...");
            }
    
            @Override
            protected Void doInBackground(Void... params) {
               // your network operation
                return null;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 12:19

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

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

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

    0 讨论(0)
提交回复
热议问题