I wanted to learn twitter connect example. For that i just downloaded a sample app from here. I just tried to run that app. When I run this app on Android 2.3.3, it is working fine but when I run it on android 4.1.2, it's giving an error NetworkOnMainThreadException
. In that code, the author has used AsynTask
background thread to perform network operations but still this error exists. Now I am confused, where is the error coming from. Please help.
There are two Solution of this Problem.
1) Don't write network call in Main UIThread
, Use Async
Task for that.
2) Write below code into your MainActivity
file after setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
And below import statement into your java file.
import android.os.StrictMode;