Thứ Năm, 10 tháng 4, 2014

Android, android.os.NetworkOnMainThreadException

When my app need to get a xml file from Internet, I meet the android.os.NetworkOnMainThreadException.

I have added <uses-permission android:name="android.permission.INTERNET"/> in my AndroidManifest.xml file but it's still not working.

But still then if you wish to force you app to do it then use the following piece of code,
if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();

            StrictMode.setThreadPolicy(policy);
        }
But again its not recommendable to use.
Source: http://stackoverflow.com/a/17939838

I found a method to fix it.
Source: http://stackoverflow.com/a/6343299
- Comments

0 nhận xét:

Đăng nhận xét