Openweathermap API - org.apache.harmony.xml.ExpatParser $ ParseException:在第1行第0列:找不到元素

时间:2014-08-19 06:42:57

标签: android android-xml saxparser weather-api openweathermap

我正在尝试使用SAX解析器http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml解析Openweathermap API。但是当我设置xmlReader.parse(new InputSource(inputStream));时,我收到了上述错误。

获取xml的代码 -

URL url = new URL(target_URL);

                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.connect();

                if(HttpURLConnection.HTTP_OK == conn.getResponseCode()){
                    SAXParserCustom saxParserCustom = new SAXParserCustom();
                    saxParserCustom.parse(conn.getInputStream());

                }

这就是我使用SAX解析XML文件的方法。

XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();

        SAXHandler saxHandler = new SAXHandler();

        xmlReader.setContentHandler(saxHandler);

        StringBuilder stringBuilder = new StringBuilder();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String s="";
        while ((s=bufferedReader.readLine())!=null){
            stringBuilder.append(s);
        }
        Log.d("SAX", stringBuilder.toString());

        xmlReader.parse(new InputSource(inputStream));

上面的日志正确打印​​xml文件所以我的猜测是我在解析函数时出错了。

0 个答案:

没有答案