android中xml解析器的响应

android中xml解析器的响应,android,xml,Android,Xml,我在android中使用SAX解析器从url获取xml数据,但仍然无法获取响应的存储位置 代码: 我的问题是响应存储在哪里,以及如何访问它以从xml获得所需的信息 编辑:非常感谢你们的帮助。。现在我明白了逻辑。。但我面临另一个问题 代码: String c = city.getText().toString(); String s = state.getText().toString(); try { SAXParserFactory factory = SAXP

我在android中使用SAX解析器从url获取xml数据,但仍然无法获取响应的存储位置

代码:

我的问题是响应存储在哪里,以及如何访问它以从xml获得所需的信息

编辑:非常感谢你们的帮助。。现在我明白了逻辑。。但我面临另一个问题

代码:

String c = city.getText().toString();
    String s = state.getText().toString();
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        HandlingXMLStuff handler = new HandlingXMLStuff();
        reader.setContentHandler(handler);
        String query = URLEncoder.encode(
        "http://api.openweathermap.org/data/2.5/weather?mode=xml&q="
                        + c + "," + s, "utf-8");
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?mode=xml&q="
                + c + "," + s);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        reader.parse(new InputSource(connection.getInputStream()));
        String information = handler.getInformation();
        output.setText(information);
    } catch (Exception e) {
        output.setText(e.toString());
    }
我没有收到来自url的响应,错误是无法将null解析为int。知道发生了什么吗??我在浏览器中输入了相同的url,得到了正确的响应,但在这里不起作用

编辑:修正。我的处理程序函数xD中出现了一些错误

feedParser oFeedParser = new feedParser(MainActivity.this);
                try {
                    /**
                    * Create a new instance of the SAX parser
                    **/
                    SAXParserFactory saxPF = SAXParserFactory.newInstance();
                    SAXParser saxP = saxPF.newSAXParser();
                    XMLReader xmlR = saxP.getXMLReader();
                    URL url = new URL("Your_URL"); // URL of the XML

                    xmlR.setContentHandler(oFeedParser);
                    xmlR.parse(new InputSource(url.openStream()));
                    handler.sendEmptyMessage(0);

                } catch(IOException e) {
                    System.out.println(e);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    System.out.println(e);
                } finally {
                    oFeedParser.closeDataBase();
                }
            }
和feedParser类

public class feedParser extends DefaultHandler {

    String elementValue = null;
    boolean elementOn, isFeedInitialized = false;
    feed oFeed;
    feedsDAO dao;

    public feedParser(Context context) {
        dao = new feedsDAO(context);
        dao.open();
    }

    public void closeDataBase() {
        dao.close();
    }

    /**
     * This will be called when the tags of the XML starts.
     **/
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (localName.equals("ITEM_NAME")) {
        } 
    }

    /**
     * This will be called when the tags of the XML end.
     **/
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (localName.equals("ITEM_NAME")) {

        }
    }

    /**
     * This is called to get the tags value
     **/
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (elementOn) {
            elementValue = new String(ch, start, length);
            elementOn = false;
        }
    }
}

检查您是否研究过Android的HTTP请求?如果没有,那么首先阅读它,以便更好地理解HTTPRequest以及如何获得它的响应
public class feedParser extends DefaultHandler {

    String elementValue = null;
    boolean elementOn, isFeedInitialized = false;
    feed oFeed;
    feedsDAO dao;

    public feedParser(Context context) {
        dao = new feedsDAO(context);
        dao.open();
    }

    public void closeDataBase() {
        dao.close();
    }

    /**
     * This will be called when the tags of the XML starts.
     **/
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (localName.equals("ITEM_NAME")) {
        } 
    }

    /**
     * This will be called when the tags of the XML end.
     **/
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (localName.equals("ITEM_NAME")) {

        }
    }

    /**
     * This is called to get the tags value
     **/
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (elementOn) {
            elementValue = new String(ch, start, length);
            elementOn = false;
        }
    }
}