【问题标题】:Parser, send an argument/receive xml (receive already done/ send not)解析器,发送参数/接收 xml(接收已经完成/不发送)
【发布时间】:2011-01-18 01:40:19
【问题描述】:
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;
}

我有这个函数接收一个 xml 并将其复制到列表中。这实施得很好。 我想知道的是发送一个类别(我像函数的参数一样接收)并且只接收来自该类别的食物。

服务器已准备好接收该类别并发送该类别的食物。
我必须做什么才能发送类别并接收正确的 xml?

【问题讨论】:

    标签: android http http-post xml-parsing http-get


    【解决方案1】:

    我想我已经弄明白了。
    还没有测试,但我认为我唯一要做的就是把猫放在网址中。

    例如:URL xpto = new URL("http://10.0.2.2/webservice/nutrituga/get_food_by_cat.php?cat="+cat);

    这对吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 2018-03-12
      相关资源
      最近更新 更多