2011-01-18 5 views
0
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 (수신 완료/수신하지 않음)

서버에서 카테고리를 수신하고 해당 카테고리의 음식을 보낼 준비가되었습니다.
카테고리를 보내고 올바른 XML을 수신하려면 어떻게해야합니까?

답변

0

나는 그것을 파악했다고 생각합니다.
아직 테스트하지는 않았지만 내가 할 일은 고양이를 URL에 넣는 것 뿐이라고 생각합니다.

예 : URL xpto = 새 URL ("http://10.0.2.2/webservice/nutrituga/get_food_by_cat.php?cat="+ cat);

이 정보가 맞습니까?