Parser, send an argument/receive xml (receive already done/ send not)

Posted by bruno on Stack Overflow See other posts from Stack Overflow or by bruno
Published on 2011-01-18T01:40:19Z Indexed on 2011/01/18 1:53 UTC
Read the original article Hit count: 492

Filed under:
|
|
|
|
public List<Afood> getFoodFromCat(String cat) {
    String resultado = "";
    List<Afood> list = new ArrayList<Afood>();

    try {
        URL xpto = new URL("http://10.0.2.2/webservice/nutrituga/get_food_by_cat.php");
        HttpURLConnection conn;

        conn = (HttpURLConnection) xpto.openConnection();
        conn.setDoInput(true);

        conn.connect();
        InputStream is = conn.getInputStream();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(is);
            NodeList nl = doc.getElementsByTagName("item");

            // resultado = String.valueOf(nl.getLength());
            for (int i = 0; i < nl.getLength(); i++) {

                Node n = nl.item(i);

                Node childNode = n.getFirstChild();

                while (childNode != null) {

                    if (childNode.getNodeType() == Node.ELEMENT_NODE) {

                        if (childNode.getNodeName().equalsIgnoreCase(
                                "NAME_FOOD")) {

                            Node valor = childNode.getFirstChild();
                            // resultado = resultado + valor.getNodeValue();
                            list.add(new Afood(valor.getNodeValue(), "",
                                    (int) Math.round(Math.random()), 1, 1,
                                    1, 1, 1, 1));
                        }
                    }
                    childNode = childNode.getNextSibling();
                }
            }
            return list;

        } catch (ParserConfigurationException e1) {
            e1.printStackTrace();
        } catch (SAXException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return list;
}

I have this function that receives a xml and copy it to the list. This is well implemented. What i want do to know, is to send a category (that i receive like an argument of the function) and receive only the food from that category.

The server is ready to receive the category and to send the food from that category.
What do i have to do to send the category and receive the correct xml?

© Stack Overflow or respective owner

Related posts about android

Related posts about http