解析器返回null

时间:2014-06-04 04:58:01

标签: android parsing rss

我正在解析rss Feed。下载流,甚至设置了解析器。但是当我将事件视为空时。此语句“int eventType = parser.getEventType();”永远是空的。 我没有任何例外。它没有阅读标签。 这是我的解析器:

    public class RssParser {
        public ArrayList<Item> parseXml(XmlPullParser parser) throws XmlPullParserException,IOException{
            ArrayList<Item> feed=null;
            Item currentFeed=null;
            int eventType=parser.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT){
                String currentTag = null;
                switch(eventType){
                case XmlPullParser.START_DOCUMENT:
                    feed = new ArrayList<Item>();

                    break;

                case XmlPullParser.START_TAG:
                    currentTag = parser.getName();
                    if(currentTag.equalsIgnoreCase("item")){
                        currentFeed = new Item();


                    }
                    else if(currentFeed != null){
                        if(currentTag.equalsIgnoreCase("title")){
                            currentFeed.title=parser.nextText();
                        }

                        else if(currentTag.equalsIgnoreCase("description")){
                            currentFeed.description=parser.nextText();

                        }

                    }
                    break;
                case XmlPullParser.END_TAG:
                    currentTag = parser.getName();
                    if(currentTag.equalsIgnoreCase("item") && (currentFeed != null)){
                        feed.add(currentFeed);
                    }
                }
                eventType = parser.next();



            }
            return feed;

        }

    }

In this class i downloaded the parser & instantiating it.

private List<Item> downloadUrl(String urlString) throws IOException{
        InputStream stream;
        List<Item> items=null;

        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 );
            conn.setConnectTimeout(15000 );
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            int response = conn.getResponseCode();  
            Log.d("response","" +response);
            stream = conn.getInputStream();
            XmlPullParserFactory pullParser;
            try{
                pullParser=XmlPullParserFactory.newInstance();
                XmlPullParser parser=pullParser.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(stream, null);
                RssParser rssObj=new RssParser();
                rssObj.parseXml(parser);
                Log.d("parser", ""+parser);
            }catch(XmlPullParserException e1){
                e1.printStackTrace();
            }
            catch (IOException e2) {
            e2.printStackTrace();   
            }
        } finally{

        }

     return items;


        }

解析器出了什么问题。我很感激你,请帮助我摆脱这个问题

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找inputstream.available()然后解析它。

由于