无法阅读RSS链接

时间:2019-11-02 04:53:14

标签: android xml xmlnodelist

我无法从此链接http://feeds.bbci.co.uk/news/world/rss.xml中获取节点“ item”。我尝试了另一个链接https://news.yahoo.com/rss/,它起作用了。但这不适用于http://feeds.bbci.co.uk/news/world/rss.xml

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lvNEWS = findViewById(R.id.lvNEWS);
    list = new ArrayList<NEWS>();
    adapter = new ListNEWSAdapter(this, list);
    lvNEWS.setAdapter(adapter);
    new RSSReader().execute("http://feeds.bbci.co.uk/news/world/rss.xml");
}

public class RSSReader extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... strings) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(strings[0]);
            InputStreamReader reader = new InputStreamReader(url.openConnection().getInputStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line);
            }
            bufferedReader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        try {
            XMLParser xmlParser = new XMLParser();
            Document document = xmlParser.getDocument(s);
            NodeList nodeList = document.getElementsByTagName("item"); //this is null
            for (int i = 0; i < 1; i++) {
                NEWS news = new NEWS();
                Element element = (Element) nodeList.item(i);
                news.setTitle(xmlParser.getValue(element, "title"));
                news.setDescription(xmlParser.getValue(element, "description"));
                news.setDate(xmlParser.getValue(element, "pubDate"));
                news.setLink(xmlParser.getValue(element, "link"));
                list.add(news);
            }
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

NodeList为空

NodeList nodeList = document.getElementsByTagName("item"); //this is null

0 个答案:

没有答案