0

I use this method for get rss items

public static ArrayList<RssItem> getRssItems(String feedUrl) {

    ArrayList<RssItem> rssItems = new ArrayList<RssItem>();

    try {
        URL url = new URL(feedUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream();

            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document document = db.parse(is);
            Element element = document.getDocumentElement();

            NodeList nodeList = element.getElementsByTagName("item");

            if (nodeList.getLength() > 0) {
                for (int i = 0; i < nodeList.getLength(); i++) {

                    Element entry = (Element) nodeList.item(i);

                    Element _titleE = (Element) entry.getElementsByTagName(
                            "title").item(0);
                    Element _descriptionE = (Element) entry
                            .getElementsByTagName("description").item(0);
                    Element _pubDateE = (Element) entry
                            .getElementsByTagName("pubDate").item(0);
                    Element _linkE = (Element) entry.getElementsByTagName(
                            "link").item(0);

                    String _title = _titleE.getFirstChild().getNodeValue();
                    String _description = _descriptionE.getFirstChild()
                            .getNodeValue();
                    Date _pubDate = new Date(_pubDateE.getFirstChild()
                            .getNodeValue());
                    String _link = _linkE.getFirstChild().getNodeValue();

                    RssItem rssItem = new RssItem(_title, _description,
                            _pubDate, _link);

                    rssItems.add(rssItem);
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return rssItems;
}

but when I set uses-sdk in manifest, after this string if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) execution proceeds to block catch. When I delete uses-sdk from manifest everything is OK. What I need to do to leave uses-sdk but it work?

boroloro
  • 133
  • 2
  • 10

1 Answers1

0

The question doesn't really describe the problem very well, but making an educated guess, you are getting NetworkOnMainThreadException.

When you don't specify a targetSdkVersion in manifest, it defaults to 1 and all backwards-compatibility features are enabled, including allowing network operations on UI thread. When you specify a target SDK version >= 11 and actually run on API level 11 or higher, you'll get NetworkOnMainThreadException.

The fix is to do network operations on a background thread using e.g. AsyncTask.

Canonical reference: How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303