0

I know that this exception is thrown when an application attempts to perform a networking operation on its main thread. I am running my code in AsyncTask.

I'm trying to connect to database in an a button's setOnClickListener How should I proceed, and avoid this Exception? I mean, how to set my code in order to execute database connection in a AsyncTask?

Gangaraju
  • 4,406
  • 9
  • 45
  • 77
BiLLiXx
  • 23
  • 1
  • 7
  • Check out tutorial on google. e.g. http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html – Elye May 08 '16 at 09:45
  • ,` how to set my code in order to execute database connection in AsyncTask`. You dont need to. The AsyncTask is for network connections. – greenapps May 08 '16 at 10:59
  • https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception?rq=1 – CommonsWare May 08 '16 at 11:35

1 Answers1

1

ADD code below in onCreate. under setContentView

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = 
    new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Hope. It will help you!!!

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22