0

私は何日もインターネットをさまよっていて、本当に助けが必要です.WebサーバーからAndroidのListViewにXMLドキュメントを解析しようとしています.ローカルファイルでそれを行う方法を考え出しましたが、それで問題ありません. 、しかし、スタックでも他のサイトでも機能していないように見えても、誰かがこれを手伝ってくれますか?? 私はページが存在し、機能していることを知っています...私は今私の髪をすべて抜いたので、助けてください:)

以下は、メソッドの私のコードです。これは、on create とアイテムの onclicklistener の後に呼び出されます。また、ドキュメント ビルダー ファクトリ メソッドを使用しています。

private void xmlparse()
{

    try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dbf.newDocumentBuilder();
            Document doc = builder.parse(new URL("URL in here").openConnection().getInputStream());

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("item");

            for (int temp = 0; temp < nList.getLength(); temp++) {

               Node nNode = nList.item(temp);
               if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                  Element eElement = (Element) nNode;

                  titles.add(getTagValue(TITLE_1, eElement));  //TITLE_1 is the xml tag title

               }
            }


          } catch (Exception e) {
            e.printStackTrace();
          }
}

private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

            Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
     }

何かばかげたことを見逃しているのでしょうか、それともボールを完全に逃してしまったのでしょうか? 誰かが私に役立つ情報を教えてくれますか、または私に知らせてくれますか:)

乾杯!!

4

1 に答える 1

1

私はそのタスクを実行するように作成しました、あなたはこれを次のように実行する必要があります:

                //Formattage des strings pour le passage en URL
                from = URLEncoder.encode(from, "UTF-8");
                to = URLEncoder.encode(to, "UTF-8");
                String addressToConnect = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+from+"&destinations="+to+"&mode=driving&units="+unit+"&sensor=false";

                //Partie connexion
                URL url = new URL(addressToConnect);
                HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
                connexion.connect();
                InputSource geocoderResultInputSource;
                geocoderResultInputSource = new InputSource(connexion.getInputStream());

                //Partie XML/XPATH

                Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
                XPath xpath = XPathFactory.newInstance().newXPath();

                //On s'assure que la requete envoy�e � l'API � bien renvoy�e un STATUS = OK cf. Google API
                NodeList nodeListCodeResult = (NodeList) xpath.evaluate("//status", geocoderResultDocument, XPathConstants.NODESET);

等...

それが役に立てば幸い、

于 2012-12-06T23:58:56.283 に答える