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.
Asked
Active
Viewed 1,919 times
0

Amir E. Aharoni
- 1,308
- 2
- 13
- 25

suresh cheemalamudi
- 6,190
- 11
- 49
- 67
-
if there is any reference to the main thread from background process then too the prob apears as it blocks the main thread post your code so others can understand where the prob is – Athul Harikumar Nov 19 '12 at 06:03
2 Answers
4
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;

Virja Rahul
- 415
- 2
- 4
- 12

Dipak Keshariya
- 22,193
- 18
- 76
- 128
-
i even tried this way. but wen i paste this code, i will get an error, assisting(Crrl+1) me to create a class by name StrictMode rather than to import android.os.StrictMode; – suresh cheemalamudi Nov 19 '12 at 06:09
-
@suresh This solution is working for me and first import above statement in your main java file and then build your project and check, it will solve your problem. – Dipak Keshariya Nov 19 '12 at 06:13
-
I am sorry. if i import this, this line(import android.os.StrictMode;) this line itself is a showing an error. Actualy i got to know to use StrictMode from many of the sites. but am not able to figure out the problem. – suresh cheemalamudi Nov 19 '12 at 06:21
-
"The import android.os.StrictMode cannot be resolved" is the error i am getting after import. – suresh cheemalamudi Nov 19 '12 at 06:24
-
@suresh see full code here and check what happened http://androidcodeexamples.blogspot.in/2011/12/how-to-integrate-twitter-in-android.html – Dipak Keshariya Nov 19 '12 at 06:26
-
I am also trying this tutorial and some more twitter android integration tutorials and result is same NOT SUCCEED. After your answer now I have a working sample project..,. THANKS A LOT..,, – MKB Nov 19 '12 at 11:48
1
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.

itsrajesh4uguys
- 4,610
- 3
- 20
- 31
-
-
i have posted the tweet also it got posted in 4.0.2 .. but i am not updated the version 4.1 so i am unable to test it... whats the issue right now you are getting . – itsrajesh4uguys Nov 19 '12 at 06:50
-
I pasted the StrictMode code posted by IGP, but wen i paste this code, i will get an error, assisting(Crrl+1) me to create a class by name StrictMode rather than to import android.os.StrictMode – suresh cheemalamudi Nov 19 '12 at 06:56
-
hi .. without strict mode why don't you tried . And my small advice you want to post the twit.. that too not continue-sly in background mode , if will post twit when user want thats it rigt – itsrajesh4uguys Nov 19 '12 at 07:06
-
my app itself is not working,(with or without StrictMode).I treid to run it both on emulator(android 4.1.2 and Nexus having 4.2). the app just crashes after start. – suresh cheemalamudi Nov 19 '12 at 07:16
-
Just now i have tested in the 4.1 Emulator it worked and posted the tweet. and have checked it in twitter site also. its works fine... have tested 4.0.3 Android Tablet . worked fine.. i am not changed any line the code which you have provided. just i have pasted my consumer and secret key . and removed one line in manifest . thats it ... – itsrajesh4uguys Nov 19 '12 at 07:20