阅读Java中的Web服务

时间:2012-10-14 01:42:04

标签: java android web-services response

我想从以下网络服务获取说明,供应商和价格。 [link] http://sagt.vizushop.com/DefaultSimple.aspx?command=item_price&id=11

响应如下

<item_details>
<description>
Jakl (Kumbuk) Center Table Timber : Mahogany/Rubber
</description>
<price>12600</price>
<vendor>BLUE</vendor>
</item_details>

我尝试使用以下示例,但它不起作用 - 为变量提供空值。

                String vendor="";
                String price="";
                String description="";
                try {

                    URL url = new URL("http://sagt.vizushop.com/DefaultSimple.aspx?command=item_price&id=11");
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(new InputSource(url.openStream()));
                    doc.getDocumentElement().normalize();

                    NodeList nodeList = doc.getElementsByTagName("description");
                    //description=nodeList.item(0).getNodeValue();
                    Node node = nodeList.item(0);

                    description = node.getNodeValue();


                    Log.v(TAG, description);
               }

1 个答案:

答案 0 :(得分:1)

如果有人需要,这就是答案。

                        URL url = new URL("http://sagt.vizushop.com/DefaultSimple.aspx?command=item_price&id=11");
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(new InputSource(url.openStream()));
                    doc.getDocumentElement().normalize();

                    NodeList nodeList = doc.getElementsByTagName("item_details");

                    Node node=nodeList.item(0);

                    Element fstElmnt = (Element) node;
                    NodeList idList = fstElmnt.getElementsByTagName("price");
                    Element idElement = (Element) idList.item(0);
                    idList = idElement.getChildNodes();
                    price=((Node) idList.item(0)).getNodeValue();

                    Element secondElmnt = (Element) node;
                    NodeList vdList = secondElmnt.getElementsByTagName("vendor");
                    Element vdElmnt = (Element) vdList.item(0);
                    vdList = vdElmnt.getChildNodes();
                    vendor=((Node) vdList.item(0)).getNodeValue();


                    Element lastElemnt = (Element) node;
                    NodeList desList = lastElemnt.getElementsByTagName("description ");
                    Element desElmnt = (Element) desList.item(0);
                    desList = desElmnt.getChildNodes();
                    description=((Node) desList.item(0)).getNodeValue();