0

我正在尝试使用 SAX 解析器解析 Openweathermap API http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml。但是当我设置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 文件,所以我的猜测是我在 parse 函数上做错了。

4

0 回答 0