在android上解析男士健康RSS时遇到问题

时间:2011-04-27 15:30:45

标签: android xml rss sax

我正在尝试使用文档构建器解析来自男士健康状况的RSS源。它无法正常工作,当我调试时,我没有收到错误。我认为问题在于元素线,因为如果我得到正确的值,我真的不明白。这是我的代码:

private void refreshTips(){
        //Get the XML
        URL url;
        try {
            String tipFeed = getString(R.string.tip_feed);
            url = new URL(tipFeed);

            URLConnection connection;
            connection = url.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            int responseCode = httpConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                InputStream in = httpConnection.getInputStream();

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

                //Parse the menshealth feed
                Document dom = db.parse(in);
                Element docEle = dom.getDocumentElement();

                //Clear the old tips
                tips.clear();

                //Get a list of each tip entry
                NodeList nl = docEle.getElementsByTagName("item");
                if(nl != null && nl.getLength() >0){
                    for(int i=0; i < nl.getLength(); i++){
                        Element item = (Element)nl.item(i);
                        Element title = (Element)item.getElementsByTagName("title").item(0);
                        Element link = (Element)item.getElementsByTagName("link").item(0);
                        Element details = (Element)item.getElementsByTagName("description").item(0);
                        Element when = (Element)item.getElementsByTagName("pubDate").item(0);

                        String _title = title.getNodeValue();
                        String _link = link.getNodeValue();
                        String _det = details.getNodeValue();
                        String _date = when.getNodeValue();

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
                        Date tDate = new GregorianCalendar(0,0,0).getTime();
                        try{
                            tDate = sdf.parse(_date);
                        }catch(ParseException e){
                            e.printStackTrace();
                        } catch (java.text.ParseException e) {
                            e.printStackTrace();
                        }

                        Tip tip = new Tip(tDate, _title, _det, _link);

                        addNewTip(tip);
                    }
                }
            }
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }
        finally {
        }
    }

    private void addNewTip(Tip tip){
        tips.add(tip);
        aa.notifyDataSetChanged();
    }

0 个答案:

没有答案