Question

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.

Was it helpful?

Solution

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;

OTHER TIPS

Did you checked by removing the android:targetSdkVersion="15" line in the Android Manifest file. Initially i am also got the issue NetworkOnMainThreadException" and have removed the line which i have mentioned above and have checked it . I am checked in 4.0.2 Please check it in 4.1.2

Hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top