Networkonmainthreadexception error

Asked

Viewed 83 times

0

People are giving this exception because by what I researched, from Android 3.0 you can no longer make network call in an Activity, but I need this to be done:

Check out my Rssfeed reading class :

public class XmlReader {

    private String rssUrl;

    /**
     * Constructor
     */
    public XmlReader(String rssUrl) {
        this.rssUrl = rssUrl;

    }

    /**
     * Pega uma lista de XML.
     * 
     * @return
     */
    public List<Segmento> getItems() throws Exception {
        // SAX parse RSS data
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XmlParseHandler handler = new XmlParseHandler();
        saxParser.parse(rssUrl, handler);
        return handler.getItems();
    }

}

I’m calling her on View thus:

RssReader reader = new RssReader("endereco.xml");

It needs to be done, but make the mistake

android.iosNetworkOnMainThreadException

. I saw that the solution was to use Asynctask, but I didn’t understand at least how to apply in my model, how would the implementation and the call?

  • See if this question/answer does not help solve the problem: http://answall.com/questions/33509/como-usar-a-biblioteca-ksoap2/33514#33514

2 answers

2

This exception occurs, as I said very well, when trying to do an operation on the Thread main. You have to run your code in a AsyncTask:

class RetrieveFeedTask extends AsyncTask<String, Void, RssReader> {

    private Exception exception;

    protected RssReader doInBackground(String... urls) {
        try {
            SAXParserFactory factory =SAXParserFactory.newInstance();
            SAXParser saxParser=factory.newSAXParser();
            XmlParseHandler handler = new XmlParseHandler();
            saxParser.parse(urls[0], handler);
            saveInBD(handler.getItems());

        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(RssReader reader) {

    }
}

Instead of

RssReader reader = new RssReader("endereco.xml");

perform the task:

new RetrieveFeedTask().execute(urlToRssFeed);

Don’t forget to add to AndroidManifest.xml this:

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

Soen source

Example

  • I had already seen this solution I could not apply in my model, 1 - because I need the getItem method and the form q ta in the example it is disappearing 2 - of the several errors, in the 1 line it has to create the class Rssfeed, and there below it does not find the rssUrl, which in my case has to be passed in the builder, can you tell how it looks exactly in my model?

  • I need the method, because I have to fetch some information from it, Ex Rssreader Reader = new Rssreader("xxxxx"); so after loading the url I need to fetch the 1 element Reader.getItem(). get(0). getId(); , using this asyncTask I won’t be able to create the object to access, and then how I do?

  • When I help, now I’m out of time.

  • War the best thing would be to store the data in a database when you read it because it is difficult to retrieve the data from an Asynctask.

  • Look at the example I set.

0

I was having the same problem, just implemented it in the onCreate method and ran normally

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.