0

私はJavaでコードを書いていて、このURLからxmlを解析しようとしています :

この URL は google API に属しており、2 つのポイント (src、dest) を受け取り、それらの間のルートを xml で返します。

プログラムをEclipseでデバッグすると、完璧に動作します。しかし、デバッグなしでコードを実行すると、エラーが返されます。(関数の最後にブレークポイントを置くと、「dist」がnullになり、理由がわかりません)なぜそれが起こるのですか?

コードは

public double calcDist(Point p) //p=the src of the ride (the dest in the calculation)
    {
        String src = Double.toString(this.lati);
        src = src.concat(",");
        src = src.concat(Double.toString(this.longi));
        String dest = Double.toString(p.lati);
        dest = dest.concat(",");
        dest = dest.concat(Double.toString(p.longi));

    String dist=null;
    URL url = null;
    try 
    {
        url = new URL("https://maps.googleapis.com/maps/api/directions/xml?origin="+src+"&destination="+dest+"&sensor=false");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        db = dbf.newDocumentBuilder();

        Document doc = null;
        doc = db.parse(new InputSource(url.openStream()));

        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("leg");

        for (int i = 0; i < nodeList.getLength(); i++) 
        {
            Node node = nodeList.item(i);
            NodeList nodeList2 =node.getChildNodes();
            for (int j = 0; j < nodeList2.getLength(); j++) 
            {
                Node child = nodeList2.item(j);
                if (child.getNodeName().contentEquals("distance"))
                {
                    NodeList nodeList3 = child.getChildNodes();
                    for (int p1 = 0; p1 < nodeList3.getLength(); p1++) 
                    {
                        Node child2 = nodeList3.item(p1);
                        if (child2.getNodeName().contentEquals("text"))
                        {
                            Node tmp = child2.getFirstChild();
                            if (tmp != null) 
                                dist = child2.getFirstChild().getNodeValue();
                        }
                    }
                }
            }
        }
    }
    catch (ParserConfigurationException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (SAXException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    dist = dist.substring(0, dist.length()-3);
    return Double.parseDouble(dist);
}
4

2 に答える 2

0

XML リクエスト json オブジェクト (https://maps.googleapis.com/maps/api/directions/json?origin=30.606595,33.546753&destination=30.657406,33.712234&sensor=false) の代わりに、http:/ /sites.google.com/site/gson/gson-user-guideを使用して Java オブジェクトに変換し、必要なものを取得します。あなたの場合、XMLは重いです。

于 2012-05-10T12:29:50.197 に答える
-1

コードは実行しません。しかし、XML ファイルを確認したところ、距離は17,0 kmxml の形式になっているように思えます。部分文字列の後にはまだ残っています17,0が、Java やその他の言語では、ドットを使用して数値を浮動させます。したがって、これはあなたのエラーである可能性があります。

しかし、パーサーの何かが間違っている可能性があります。

低速でパフォーマンスの低い DOM パーサーの代わりに、Java の SAX-Parser API を XML 解析に使用できます。

ブラジル

于 2012-05-10T12:30:27.170 に答える