Login Twitter to show the error "android.os.NetworkOnMainThreadException"

you have to check this onCreate() method:

if (android.os.Build.VERSION.SDK_INT > "YOur minimum SDK Version here") {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

And add permission to manifest..

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Update

android.os.NetworkOnMainThreadException means you are performing your network operation on main thread which it will be blocked. So above solution is to restrict network error. But to overcome with this error you should use AsyncTask or by creating child thread and update your UI in runOnUiThread method which will update UI in that method.


This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:

class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
        try {
            URL url= new URL(urls[0]);
            SAXParserFactory factory =SAXParserFactory.newInstance();
            SAXParser parser=factory.newSAXParser();
            XMLReader xmlreader=parser.getXMLReader();
            RssHandler theRSSHandler=new RssHandler();
            xmlreader.setContentHandler(theRSSHandler);
            InputSource is=new InputSource(url.openStream());
            xmlreader.parse(is);
            return theRSSHandler.getFeed();
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

How to execute the task:

new RetreiveFeedTask().execute(urlToRssFeed);

Don't forget to add this to AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>

Just add the following code in the oncreate off Activity:

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